开发者

Adding items to a multi-dimensional array during query while loop

I have a query returning data that looks like this:

Status         Total
Success        234
Failed         20
Missing        12

I want to add this to an array which can then be used to populate a google pie chart.

the array would look like this:

array backup = array("Success" => 234),
                    ("Failed" => 20),
                    ("Missing" => 12);

H开发者_开发技巧ow would I add these item dynamically at each row in a query?


$result = mysql_query(...);
$backup = array();

while ($r = mysql_fetch_assoc($result)) {
    $backup[$r['Status']] = $r['Total'];
}

Here's how you can make the Google Charts API call:

$values = implode(',', array_values($backup));
$labels = implode('|', array_keys($backup));

$img = "http://chart.apis.google.com/chart?cht=p3&chd=t:{$values}&chl={$labels}&chs=250x100";
echo "<img src='{$img}' alt='Chart'>";


Assuming this is your query:

SELECT status, total FROM table

Then you can do:

$data = array();

while(($row = mysql_fetch_assoc($result))) {
    $data[$row['status']] = $row['total'];
}

If this is not what you mean, please clarify your question and/or provide the code you already have.


I think we need a bunch more information, but in the mean time look at array_merge()

http://www.php.net/manual/en/function.array-merge.php

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜