0

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?

1
  • Just what are you trying to do? Also, the second code block does not contain valid php, further obfuscating what you want done... I really can't make out what you are trying here... Commented Aug 25, 2010 at 16:24

3 Answers 3

2
$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'>";
Sign up to request clarification or add additional context in comments.

2 Comments

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!
@Jonesy You are welcome, but those aren't arrays. implode() returns a string.
1

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.

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.