1

Please excuse my newbie Question.

I'm tryin to display data from a mysql table into a chart (phpChart http://phpchart.net) What I need is to display all values of Field cost for Region 'CA'

E.g

ID  Cost  Region
----------
1   500    CA
----------
2   100    DP
----------
3   280    CA
----------
4   40     ST
----------
5   80     CA
----------

<?php
include 'config.php';
$query = "SELECT Cost From tblForecast where Region='CA' =";
$rs = mysql_query($query);

if (!$rs) {
    echo "Could not execute query: $query";
    trigger_error(mysql_error(), E_USER_ERROR); 
} else {
    echo "Query: $query executed\n";
} 

$nrows = mysql_num_rows($rs);

for ($i = 0; $i < $nrows; $i++) {
    $row = mysql_fetch_row($rs);
    echo $row[0];
    echo "<br/>";
}

$pc = new C_PhpChartX(array(array($row[0])),'basic_chart');
\\"This where i'm stuck as to how display the other values within the array"

$pc->set_title(array('text'=>'Basic Chart with Bar Renderer'));
$pc->set_series_default(array('renderer'=>'plugin::BarRenderer'));

$pc->draw();
mysql_close();

?>

I'm able to display the first rows value in the chart only.

Anyone's help would be much appreciated Thanks in advance

4
  • is your query string wrong? it looks like an additional quote and equal sign Commented Jun 22, 2012 at 17:28
  • what does $nrows return? 1 or.... many? Commented Jun 22, 2012 at 17:28
  • 1
    Never heard of phpchart. Looks really useful. Commented Jun 22, 2012 at 17:31
  • Yeah thanks i did have a typo there, @CountMurphy they fairly new but look really good and have quite a few features. Commented Jun 22, 2012 at 18:03

2 Answers 2

4

You're continually fetching your query results into a single variable, and overwriting that data on each iteration of the loop.

Try this:

$data = array();
while ($row = mysql_fetch_array($rs)) {
   $data[] = $row[0];
}

$pc = new C_PhpChartX($data, 'basic_chart');

Plus, though this is most likely on a cut/paste type, you've got a syntax error in your query anyways:

$query = "SELECT Cost From tblForecast where Region='CA' =";
                                                         ^---syntax error
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it seems to work when i use $pc = new C_PhpChartX(array(array($data[0],$data[1],$data[2])),'basic_chart'); Is this right, if so how could I do it with out stipulating the array index. i.e show all data Yes my mistake that was a typo $query = "SELECT Cost From tblForecast where Region='CA' =";
just array($data), maybe, if this chart library requires an array-of-arrays.
Ok, think you might be right this is an example of one of their charts: $pc = new C_PhpChartX(array(array(11, 9, 5, 12, 14)),‘basic_chart’); $pc->draw(); Thanks for the assistance, a step closer in the right direction,
0

You could use GROUP_CONCAT()

include 'config.php';
$query = "SELECT GROUP_CONCAT(Cost) cost
         From tblForecast
         where Region='CA'";
$rs = mysql_query($query);
$row = mysql_fetch_row($rs);
echo $row['cost']

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.