Forum Moderators: coopster

Message Too Old, No Replies

inserting mysql result in array

inserting mysql result in array to be used in phplot.

         

kenchix1

9:08 am on Dec 5, 2006 (gmt 0)

10+ Year Member



The phplot says that I need to pass the data into an array so it would look like this :


(excerpt from phplot manual)

$example_data = array(
array('a',3),
array('b',5),
array('c',7),
array('d',8),
array('e',2),
array('f',6),
array('g',7)
);

but my data is dynamic and it comes from MySQL table and I need to put it inside an array.


$vtarget=$_GET['s'];

$gquery="select ddate,score from data1 where target='$vtarget'";
$gresult=mysql_query($gquery);
$dbres = mysql_fetch_assoc($gresult);

$graph_data = array(
foreach($dbres as $result)
{
array('$result['ddate']',$score),
}
);
$graph_data=$array;

$plot->SetDataValues($graph_data);

this is my code and it is not working no matter how I modify it (im a newbie to php). How do I insert the value of 'ddate' and 'score' inside the array so I can pass it to phplot and create a chart?

I also noticed that if I put it inside the foreach loop, it will give an error because the last element should not have a comma in the end (,).

Thanks in advance.

kenchix1

9:33 am on Dec 5, 2006 (gmt 0)

10+ Year Member



I also tried :

$graphdata = mysql_fetch_row($gresult);

but it display nothing, it only says "No Data".

phranque

10:32 am on Dec 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i speak perl so i'm not sure of the php syntax and variable interpolation rules - have you printed out the query to make sure it is what you think it should be?
have you tried pasting that query into a mysql client or using the command line to see the query results?

kenchix1

10:54 am on Dec 5, 2006 (gmt 0)

10+ Year Member



thanks for the suggestions. the query is running fine and gives proper result. I was able to find out how it could put the result by using this:

while ($row = mysql_fetch_row($gresult))
{
$graph_data[]=$row;
}

now my problem is how to make a multi-dimensional array with 3 - 10 elements in each row.

thanks again.

mcibor

11:01 am on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first problem is that you take only one result. Moreover you don't assign it nowhere

//...
$gquery="select ddate,score from data1 where target='$vtarget'";
$gresult=mysql_query($gquery);
$dbres = mysql_fetch_assoc($gresult); //only one row

$graph_data = array( //you can't run loops in declarations
foreach($dbres as $result)
{
array('$result['ddate']',$score));//You write to array text $result... not the variable. But as you don't assign the array it is not used anywhere
}
);
$graph_data=$array; //variable $array is empty - this is the first time it appears

$plot->SetDataValues($graph_data);


Your code should be

//...
$gquery="SELECT `ddate`, `score` FROM `data1` WHERE `target`='$vtarget'";
$gresult=mysql_query($gquery);

$graph_data = array();

while($dbres = mysql_fetch_assoc($gresult)) //take all results
{
$graph_data[] = array($dbres['ddate'],$dbres['score']);//No ' around variable. $var[] creates new element of an array.
}

$plot->SetDataValues($graph_data);

Hope this helps and cleares everything
Michal

mcibor

11:09 am on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$mult = array(
1 => "single",
2 => array("a1" => "double1", "a2" => "double2"),
3 => array("b1" => array("c1" => "triple")
);

echo $mult[1]; // -> single
echo $mult[2]["a2"]; // -> doubl2
echo $mult[3]["b1"]["c1"]; // -> triple

Hope this helps

kenchix1

2:48 pm on Dec 5, 2006 (gmt 0)

10+ Year Member



thank you for the help. I'll be getting my data for the chart in a table so the format of the data will look like this :

aaa
1
3
3
4
2
6
7

bbb
1
2
3
4
3
2

ccc
10
8
9
6
8
9
4
3

so on. it will go up to 10.

my problem is how to put it in a multidimensional array so I can plot it in a chart.
something like this

aaa,1,3,3,4,2,6,7
bbb,1,2,3,4,3,2
ccc,10,8,9,6,8,9,4,3
...

as you will also notice, the numbers is not fixed.

thanks again.

mcibor

4:17 pm on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do that in loops if you like:


$chart = array();

for($j = 1; $j <= 3; $j++)
{
for($i = 1; $i <= 10; $i++)
{
$chart[$j][$i] = get_chart_data($i, $j);
}}

or if you have data in mysql, you can:


$chart = array();
$vars = array("x", "y", "z");
$i = 0;
$j = 0;

while($row = mysql_fetch_assoc(...))
{
if($i = 10)
{
$j++;
$i = 0;
}
$chart[$vars[$j]][$i] = $row['value'];
$i++;
}


Hope this helps
Michal

kenchix1

2:18 am on Dec 6, 2006 (gmt 0)

10+ Year Member



thanks again for the help. I hope I can do it. I'll post again if I encounter problem or if I succeed.

thanks.