views:

29

answers:

3

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

How would I add these item dynamically at each row in a query?

A: 

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

+2  A: 
$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'>";
NullUserException
Wow thank you so much! everything there is exactly what I needed! I never new you could use array in the charts API. thanks a bunch NullUserException!
iamjonesy
@Jonesy You are welcome, but those aren't arrays. `implode()` returns a string.
NullUserException
+1  A: 

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.

Felix Kling