【问题标题】:create monthly archive with PHP and MySQL使用 PHP 和 MySQL 创建每月存档
【发布时间】:2013-06-25 01:49:08
【问题描述】:

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

$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

【问题讨论】:

    标签: php mysql while-loop archive


    【解决方案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']}";
      }
     }
    

    【讨论】:

    • 试试这个,它会工作的。并且请检查我写的SQL语句中的列名。
    【解决方案2】:

    您可以像这样在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>";
      }
    }
    

    【讨论】:

    • 谢谢,但我如何才能在该列表中显示每个月的所有新闻?我的意思是,我如何从 MySQL 获取该月的数据?
    • 我已经编辑了我的代码,我添加了一条评论,希望你能得到它。您可以在while循环中添加sql查询以获取每个月的数据。
    【解决方案3】:

    使用 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>";
        }
    }
    

    enter image description here

    关于输出图像。

    输出

    【讨论】:

      【解决方案4】:

      使用 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>    
      

      【讨论】:

      • 同一个问题有两个答案?请选择一个:-)另一个响应有图像,输出什么都不做?请查看。
      猜你喜欢
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多