开发者

mySQL query & php?

We are stuck performing this mySQL query and the PHP along side it.

Here is our query:

 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

Here is the PHP:

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

Here is a sample output:

Array
(
    [0] => 2011-10-10
    [theday开发者_如何学JAVA] => 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
)

Basically, we want to display the results like this, in a while loop, for each day:

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

We are stuck on how to do the query & the PHP to get the results displayed like this.

We basically want the above example, to be looped 14 times (for the last 14 days), and output like the above example, so it groups the count & status for each day and echos it out like so.

Thank you.


You would need to build an array of date to status data. For example

$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'];
}

and to display...

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


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


Phils array will work good. You could also do it manually like this.

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

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

    }

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


Try

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜