【问题标题】:PHP Blog, need to make a good looking archives sectionPHP博客,需要做一个好看的档案区
【发布时间】:2010-11-06 19:59:40
【问题描述】:

所以!基本上我有一个包含大量博客文章的数据库,这些都是按 UNIX 时间戳排序的,我需要一种方法让这段代码在适当的时候吐出标题,这样它就会输出如下内容:

2008

11 月

标题 1 - 日期在这里
标题 2 - 日期在这里

十二月

标题 3 - 日期在这里

2009

一月

标题 4 - 日期在这里

等等

到目前为止,这是我的代码,它在比较年份之前一直有效,我仍然需要想出一个好方法来让它以合理的方式比较月份,这样一月确实在十二月之后,并且不是什么可笑的第 13 个月。

[代码]

<?php   
   if ($db = new PDO('sqlite:./db/blog.sqlite3')) {
         $stmt = $db->prepare("SELECT * FROM news ORDER BY date DESC");
         if ($stmt->execute()) {
               while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
                     $current_year = date("Y", $row[1]);
                     $current_month = date("m", $row[1]);
                     if ($current_year > $last_year) {
                           echo "<h1>" . $current_year . "</h1>";
                           $last_year = $current_year;
                     }
                     echo "<tr>";
                     echo "<td align='left'><a href='view_post.php?post_id=". $row[1] ."'>" . $row['0'] . " - " . date("Y-m-d, H:i:s", $row[1]) . "</a></td>";
                     echo "</tr>";
               }
         }
   } else {
         die($sqliteerror);
   }
?>

[/代码]

【问题讨论】:

    标签: php blogs archive


    【解决方案1】:

    使用 unix 时间戳,您可以执行类似的操作(显然是伪代码)

    prev_month = null
    prev_year = null
    foreach results as r
        new_month = date('F', r[timestamp]);
        new_year = date('Y', r[timestamp]);
        if(prev_month != new_month)
            //display month
        /if
    
        if(prev_year != new_year)
            //display year
        /if
    
        // display other info
    
        prev_month = new_month
        prev_year = new_year
    /foreach
    

    【讨论】:

    • 没有问题,很抱歉这是伪代码,但我更喜欢让人们学习而不是马上给他们完整的答案
    • 不,实际上,这是最好的方法,因为它让我更容易适应我的代码。 (例如,我使用“while 循环”而不是“for 循环”)很棒的东西,很棒的东西。
    【解决方案2】:
    <?php   
       if ($db = new PDO('sqlite:./db/blog.sqlite3')) {
             $stmt = $db->prepare("SELECT * FROM news ORDER BY date DESC");
             if ($stmt->execute()) {
                   while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
                         $current_year = date("Y", $row[1]);
                         $current_month = date("n", $row[1]);   # n instead of m
                         if ($current_year > $last_year) {
                               echo "<h1>" . $current_year . "</h1>";
                               echo "<h2>" . $current_month . "</h2>";
                               $last_year = $current_year;
                               $last_month = $current_month;
                         }
                         elseif ($current_month > $last_month) {
                               echo "<h2>" . $current_month . "</h2>";
                               $last_month = $current_month;
                         }
                         echo "<tr>";
                         echo "<td align='left'><a href='view_post.php?post_id=". $row[1] ."'>" . $row['0'] . " - " . date("Y-m-d, H:i:s", $row[1]) . "</a></td>";
                         echo "</tr>";
                   }
             }
       } else {
             die($sqliteerror);
       }
    ?>
    

    我没有测试它。 主意: 如果年份改变,月份也会改变。 下个月的检查只有在年份没有变化的情况下才会发生,这是上个月可能大于当前月份的唯一情况。

    【讨论】:

      【解决方案3】:

      我很想分两步完成,将数据库获取与显示/html 逻辑分开,例如:

      <?php
      //snip
      
      //make big array of archive
      $archive = array();
      while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
          $year = date("Y", $row[1]);
          $month = date("m", $row[1]);
      
          if (!isset($archive[$year])) {
              $archive[$year] = array();
          }
      
          if (!isset($archive[$year][$month])) {
              $archive[$year][$month] = array();
          }
      
          $archive[$year][$month][] = $row;
      }
      //snip
      
      //loop over array and display items 
      ?>
      
      <?php foreach ($achive as $year => $months): ?>
          <h1><?php echo $year; ?></h1>
      
          <?php foreach ($months as $month => $posts): ?>
               <h2><?php echo $month; ?></h2>
               <ul>
               <?php foreach ($posts as $post): ?>
                   <li><?php echo $post[0]; ?> etc...</li>
               <?php endforeach; ?>
               </ul>
          <?php endforeach; ?>
      <?php endforeach; ?>
      

      您应该根据您的 SQL 查询以相反的顺序获取年份和月份。这个我没有测试过,但应该是正确的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        • 2010-12-24
        • 2020-08-28
        • 1970-01-01
        相关资源
        最近更新 更多