0

I am using PHPlot to make a graph.

I have an issue in generating the array from a MySQL table.

Basivally, I want to array is as follows:

$values = array($arrayx);
array('a',-3),
array('b',5),
array('c',7),
array('d',8),
array('e',12),
array('f',-6),
//);

$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');

Part of the code where I tried to retrieve values from table to feed the array

$query="SELECT * FROM tasks";
$result=mysql_query($query);

//using a for loop to add values to the array
while ($resource=mysql_fetch_array($result)){
$thedate = $resource["date"];
$title = $resource2["title"];

$innerarray = "array('.$thedate.', $title),";

}
$values = array($innerarray).");";

$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');

//Draw it
$graph->DrawGraph();
  }

The way I'm doing the $innerarray and $values seems wrong. Can you please help me fix it?

Thank you

user1581579
  • 323
  • 3
  • 7
  • 14
  • please consider to accept an answer (click tick mark on the left) if it actually answered your question – michi Apr 14 '13 at 13:19

2 Answers2

0

I assume it is this that you want:

  $sql="SELECT datefield, titlefield FROM tasks";

  ....     

  while (list($thedate,$thetitle) = mysql_fetch_array($result)) {

      $values[] = array($thedate,$thetitle);

 }

 echo $values[0][0]; // will output your 1st date
 echo $values[0][1]; // will output your 1st title
michi
  • 6,565
  • 4
  • 33
  • 56
  • +michi Something like this but how to do I select 2 columns from my tables only and assign $thedate and $thetitle to column date and column title? Thanks. – user1581579 Feb 20 '13 at 15:49
  • @user1581579: use @ instead of + before the name in comments. I edioted my answer, is this what you want? if not, please provide your table-structure in your question. – michi Feb 20 '13 at 16:00
  • the database has "date" "title" "time" as column. I tried the following: while (list($thetitle,$thedate) = mysql_fetch_array($result2)) { $values[] = array($thetitle,$thedate); } echo $values; When I echo values, I get "Array" displayed. No actual values. – user1581579 Feb 20 '13 at 16:22
  • @user1581579: what should $values look like? – michi Feb 20 '13 at 16:25
0

try replacing

$innerarray = "array('.$thedate.', $title),";

with

$innerarray = array($thedate, $title);
$new = array();
while(for condition ){
$new[] = '\''.thedate[$i].''\','.$title[$i].'\';
}

var_dump($new);

this an idea, you need to edit the code to make it working

Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77
  • I tried the above and got Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 71 bytes) in..... – user1581579 Feb 20 '13 at 16:03