2015-04-02 117 views
-1

我在我的網站上創建了一個檔案導航,並找到了一些關於如何做到這一點的文章,並將一些代碼放在一起,但似乎無法使其工作。博客檔案

我希望它顯示,如:

January 2015 (9) 
December 2014 (8) 
November 2014 (10) 
October 2014 (15) 
... 

下面的代碼:

$sql = "SELECT YEAR(timestamp_published) AS 'year', Month(timestamp_published) AS 'month', COUNT(id) AS 'count' FROM table GROUP BY YEAR(timestamp_published), MONTH(timestamp_published) DESC"; 
$data = $db->query($sql); 

while($row = $db->fetch_assoc()) { 
    $data[$row['year']][$row['month']] = $row['count']; 
} 

foreach ($data as $year => $months) { 
echo archive_date($month).' '; 
    foreach ($months as $month => $count) { 
    echo $year.' ('.$count.') (' . $data->id . ')<br/>'; 
} 
    } 
} 

謝謝!我最終解決了我自己的問題。我是在一堂課中建立這個課題的,有幾個問題。我現在已經有了代碼工作,在這裏它適用於將來的任何人。

$sql = "SELECT YEAR(timestamp_published) AS 'year', MONTH(timestamp_published) AS 'month', COUNT(`publish`) AS 'count' FROM ".static::$table_name." GROUP BY YEAR(timestamp_published) DESC, MONTH(timestamp_published) ASC"; 
$result = $db->query($sql); 
$data = array(); 
while($row = $result->fetch_assoc()) { 
    $data[$row['year']][$row['month']] = $row['count']; 
} 

foreach ($data as $year => $months) { 
    echo '<strong>'.$year.'</strong><br/><br/>'; 
foreach ($months as $month => $count) { 
    $dateObj = DateTime::createFromFormat('!m', $month); 
    $monthName = $dateObj->format('F'); // March 
    echo $monthName . ' ('.$count.')<br/>'; 
} 
    echo '<hr/>'; 
} 

實際上,它找出來是這樣的:

2015年
一月(3)
二月(5)
三月(6)
...

January(7)
February(9)
...

+2

是什麼您提供的代碼的輸出?任何錯誤? – 2015-04-02 07:28:17

回答

0

我認爲你正在尋找獲得一個月的字符串表示(有點不清楚),但可以使你的情況下,使用date()像這樣

date ("F", $month) 

實現

echo date ("F", $month) . ' ' . $year.' ('.$count.')<br/>';