2

我正在尝试在我的网站上创建一个新闻档案。我写了这段代码:

$sql_result = $db->query("
SELECT *,COUNT(id) AS itemCount 
FROM post 
GROUP BY DATE_FORMAT(date, '%Y-%m') DESC ");

while ( $row = $db->get_row( $sql_result ) ) {
 $datetime = strtotime($row['date']);
 $tday = date("Y-m", $datetime);
 $count = $row['itemCount'];
 echo "Month: {$tday} - News Number: {$count}<br>";
}

这是结果:

Month: 2013-06 - News Number: 4
Month: 2013-05 - News Number: 3
Month: 2013-04 - News Number: 4
Month: 2013-03 - News Number: 3

我的问题是,如何在该月之后的每个月显示新闻标题?例如这样的:

Month: 2013-06 - News Number: 4
 -news number 1 for this month
 -news number 2 for this month
 -news number 3 for this month
 -news number 4 for this month
Month: 2013-05 - News Number: 3
 -news number 1 for this month
 -news number 2 for this month
 -news number 3 for this month
Month: 2013-04 - News Number: 4
 -news number 1 for this month
 -news number 2 for this month
 -news number 3 for this month
 -news number 4 for this month
Month: 2013-03 - News Number: 3
 -news number 1 for this month
 -news number 2 for this month
 -news number 3 for this month
4

4 回答 4

1

尝试使用此代码。

 while ( $row = $db->get_row( $sql_result ) ) {
  $datetime = strtotime($row['date']);
  $tday = date("Y-m", $datetime);
  $count = $row['itemCount'];
  echo "Month: {$tday} - News Number: {$count}<br>";
  $sql_result1 = $db->query("
  SELECT newsTitle 
  FROM post 
  WHERE DATE_FORMAT(date, '%Y-%m')=DATE_FORMAT({$datetime}, '%Y-%m')");
  while($row1 = $db->get_row( $sql_result1 ) ){
   echo "News: {$row1['newsTitle']}";
  }
 }
于 2013-06-27T14:40:33.487 回答
0

您可以在循环中添加一个循环while,如下所示;

while ( $row = $db->get_row( $sql_result ) ) {
  $datetime = strtotime($row['date']);
  $tday = date("Y-m", $datetime);
  $count = $row['itemCount'];
  echo "Month: {$tday} - News Number: {$count}<br>";

  /////////////////////////
  // You can add a sql query here, as you added before while loop, to get each
  // month's data on the basis of unique-id which you have in '$row'
  /////////////////////////

  for($i=1; $i<=$count; $i++)
  {
    echo "-news number {$i} for this month<br>";
  }
}
于 2013-06-27T14:22:41.020 回答
0

使用 PHP 和 MySQL 测试年度存档的完整示例(如博客)

<link href="CSS/treeview.css" rel="stylesheet" />
    <script src="scripts/jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            //start the tree in an autocollapsed state
            $('#Decor ul').hide(400);

            $('#Decor li').on('click', function (e) {
                e.stopPropagation(); // prevent links from toggling the nodes
                $(this).children('ul').slideToggle();
            });

            // This code opens all hyperlinks in a new window 
            // and avoids anchors
            $('#Decor a').not('[href="#"]').attr('target', '_blank');
        });
    </script>
</head>

<?php
define("DB_USER","");
define("DB_PASS","");
define("DB_HOST","");
define("DB_NAME","");

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_errno());
$s="SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
$sql_result=mysqli_query($link,$s);
?>
<ul id="Decor">
<?php
while($row=mysqli_fetch_array($sql_result))
    {

    $datetime=strtotime($row['date_upload']);
    $tday = date("Y", $datetime);
    $count = $row['itemCount'];

    ?>
     <li class="level1"><?php echo "<u><strong>{$tday} ({$count})</strong></u><br>"; ?>
    <?php
        $s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";
        $q=mysqli_query($link,$s1);
        while($month=mysqli_fetch_row($q))
        {
        ?>
            <ul>
               <li><a href="#"><?php echo date("M",strtotime($month[5]))."<br>"; ?></a></li>
            </ul>
            <?php


        }

    echo "<br>";

}
?>
</ul>
</html>    
于 2017-11-21T11:42:32.743 回答
0

使用 PHP 和 MySQL 创建年度存档

  $link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_errno());
$s="SELECT *,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload, '%Y') DESC ";
$sql_result =mysqli_query($link,$s);

while ($row = mysqli_fetch_array($sql_result))
    {
    $datetime = strtotime($row['date_upload']);
    $tday = date("Y", $datetime);
    $count = $row['itemCount'];
    echo "Month: {$tday} - News Number: {$count}<br>";

    for($i=1; $i<=$count; $i++)
    {
    echo "-news number {$i} for this month<br>";
    }
}

在此处输入图像描述

关于输出图像。

输出

于 2017-11-21T09:56:57.187 回答