2

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

2008年

十一月

标题 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);
   }
?>

[/代码]

4

3 回答 3

2

使用 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
于 2009-06-29T21:55:15.113 回答
0

我很想分两步执行此操作,将数据库获取与显示/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 查询以相反的顺序获取年份和月份。我没有测试过这个,但它应该是正确的。

于 2009-06-29T21:58:02.610 回答
0
<?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);
   }
?>

我没有测试它。想法:如果年份改变,月份也会改变。仅当年份没有变化时才会检查下个月,这是上个月可能大于当前月份的唯一情况。

于 2009-06-29T22:08:22.997 回答