2

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] => 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.

4
  • 1
    Harsh downvote, this is a well written question. +1 to counter :)
    – Phil
    Oct 10, 2011 at 9:24
  • 1
    suggestions: use mysql_fetch_assoc($q) OR mysql_fetch_array($q, MYSQL_ASSOC) in while` loop
    – xkeshav
    Oct 10, 2011 at 9:32
  • 1
    FYI, spaces in column names, yuck!
    – Phil
    Oct 10, 2011 at 9:50
  • @Phil, I didn't create the site, I'm doing this for a friend ^_^
    – Latox
    Oct 11, 2011 at 1:27

4 Answers 4

2

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>
2
  • I've done this, and it just echos out a heap of numbers, I still have no idea how to display the data like shown in my question. Sorry for being stupid >.< Thanks for the help so far.
    – Latox
    Oct 10, 2011 at 9:33
  • This is perfect, thank you so much. There are some really smart people on here :)
    – Latox
    Oct 10, 2011 at 9:44
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
2
  • I assume this is to cater for zero occurrence statuses? Would have to also consider that there are potentially more status names than shown. Not sure how you would deal with that.
    – Phil
    Oct 10, 2011 at 9:53
  • @Phil: intended to illustrate how to solve the problem (or at least one method) - more complete solutions for implementing pivot tables using MySQL available on internet. e.g. artfulsoftware.com/infotree/queries.php#523
    – symcbean
    Oct 10, 2011 at 16:18
-2

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>";
    }
1
  • You're completely ignoring the date and status count data
    – Phil
    Oct 10, 2011 at 9:46
-4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.