2013-06-27 38 views
2

我正在嘗試在我的網站中創建新聞存檔。我寫了這個代碼:使用PHP和MySQL創建月度存檔

$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 

回答

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']}"; 
    } 
} 
+0

謝謝,但它不工作... – Alireza

+0

試試這個,它會工作。請檢查我寫的SQL語句中的列名。 –

+0

非常感謝你我的親愛的朋友。工作中。 – Alireza

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>"; 
    } 
} 
+0

感謝,但我怎麼能顯示從該列表中每個月的所有消息嗎?我的意思是,我怎麼能從那個月的MySQL獲取數據? – Alireza

+0

我編輯了我的代碼,我添加了一條評論,我希望你能得到它。您可以添加sql查詢以獲取while循環中的每個月的數據。 – JazyK

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

enter image description here

關於輸出圖像。

輸出

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>  
+0

關於同一問題的兩個答案?請選擇一個:-)另一個響應有圖像,輸出什麼都不做?請查閱。 – Nic3500