2011-10-10 63 views
2

我們一直在執行這個mySQL查詢和PHP。mySQL query&php?

這裏是我們的查詢:

SELECT DATE(`Checked`) AS theday, COUNT(`Download ID`) AS thecount, 
`Status` AS thestatus 
FROM `download` 
WHERE `Checked`>= (CURRENT_DATE - INTERVAL 14 DAY) 
GROUP BY theday, thestatus ORDER by theday DESC 

這裏是PHP:

while ($r = mysql_fetch_array($q)){ 
    echo "<pre>"; 
    print_r($r); 
    echo "</pre>"; 
} 

下面是一個示例輸出:

Array 
(
    [0] => 2011-10-10 
    [theday] => 2011-10-10 
    [1] => 1 
    [thecount] => 1 
    [2] => Downloading 
    [thestatus] => Downloading 
) 


Array 
(
    [0] => 2011-10-10 
    [theday] => 2011-10-10 
    [1] => 9 
    [thecount] => 9 
    [2] => Converting 
    [thestatus] => Converting 
) 

Array 
(
    [0] => 2011-10-10 
    [theday] => 2011-10-10 
    [1] => 2673 
    [thecount] => 2673 
    [2] => Complete 
    [thestatus] => Complete 
) 

Array 
(
    [0] => 2011-10-10 
    [theday] => 2011-10-10 
    [1] => 366 
    [thecount] => 366 
    [2] => Aborted 
    [thestatus] => Aborted 
) 

基本上,我們要顯示的結果類似這個,在一個while循環中,每天:

2011-10-10 
Downloading: 1 
Converting: 9 
Complete: 2673 
Aborted: 366 
Error: 0 

我們被困在如何執行查詢&的PHP來獲得結果顯示像這樣。

我們基本上要在上面的例子中,將循環14次(最近14天),輸出像上面的例子,所以這組每天和回聲出來,像這樣的數&狀態。

謝謝。

+1

惡劣downvote,這是一個很好的書面質詢。 +1到計數器:) – Phil

+1

意見建議:在'while'循環中使用'mysql_fetch_assoc($ q)'或'mysql_fetch_array($ q,MYSQL_ASSOC) – diEcho

+0

謝謝,不知道爲什麼我被downvoted了,但是哦。 – Latox

回答

2

您將需要生產日期的數組狀態數據。例如

$dates = array(); 
while ($r = mysql_fetch_assoc($q)) { 
    if (!array_key_exists($r['theday'], $dates)) { 
     $dates[$r['theday']] = array(); 
    } 
    $dates[$r['theday']][$r['thestatus']] = $r['thecount']; 
} 

,並顯示...

<dl> 
    <?php foreach ($dates as $date => $status) : ?> 
     <dt><?php echo htmlspecialchars($date) ?><dt> 
     <?php foreach ($status as $key => $count) : ?> 
     <dd><?php printf('%s: %d', 
      htmlspecialchars($key), $count) ?></dd> 
     <?php endforeach ?> 
    <?php endforeach ?> 
</dl> 
+0

我這樣做了,它只是回聲一堆數字,我仍然不知道如何顯示數據就像我的問題所示。對不起,很愚蠢>。<感謝您的幫助。 – Latox

+0

@Latox我已經用一些顯示代碼更新了我的答案 – Phil

+0

這很完美,非常感謝。這裏有一些非常聰明的人:) – Latox

-4

嘗試

echo "<pre>"; 
$first = true; 
while ($row = mysql_fetch_array($q)){ 
    if ($first) echo $row['theday'] . "\n"; 
    echo $row['thestatus'] . ": " $row['thecount'] . "\n"; 
    $first = false; 
} 
echo "</pre>"; 
-2

菲爾斯陣列將工作良好。你也可以像這樣手動完成。

while($r = mysql_fetch_array($q)) { 

$status[$r['thestatus']] = $status[$r['thestatus']] +1; 

    } 

    foreach($status as $key => $value) { 
    print $key.":".$value."<br>"; 
    } 
+0

你完全忽略日期和狀態計數數據 – Phil

1
SELECT DATE(`Checked`) AS theday, 
SUM(IF(Status='Downloading', 1, 0)) as downloading, 
SUM(IF(Status='Converting', 1, 0)) as converting, 
SUM(IF(Status='Complete', 1, 0)) as complete, 
SUM(IF(Status='Aborted', 1, 0)) as aborted, 
SUM(IF(Status='Error', 1, 0)) as error 
FROM `download` 
WHERE `Checked`>= (CURRENT_DATE - INTERVAL 14 DAY) 
GROUP BY theday 
ORDER by theday DESC 
+0

我認爲這是爲了滿足零發生狀態?還必須考慮到可能比所示的地位更多。不知道你會如何處理。 – Phil

+0

@Phil:旨在說明如何**解決問題(或至少一種方法) - 更完整的解決方案,使用互聯網上的MySQL實現數據透視表。例如http://www.artfulsoftware.com/infotree/queries.php#523 – symcbean