Page 1 of 1

Mysql Results in Multidimensional Array

Posted: Wed May 26, 2004 8:15 am
by Etherguy
I am trying to output mysql results into a multidimensional array so I can do some charting with it. I can't seem to get the data in the format I need. Which is :

Code: Select all

<?php
$chart[ 'chart_data' ] = array ( array ( "", "Dan", "Gary", "Tom", "Al", "Pete" ), array ( "", 63, 40, 10, 1, 0 ) );
?>

Basically I have 2 fields... a name field and a numeric field. If I manually do the MD-Array it works like a champ. If I try to use data from mysql is dies or loops forever.

I have tried imploding the data, which works fine when I am not trying to create a MD-Array.

Any ideas?

Posted: Wed May 26, 2004 8:28 am
by magicrobotmonkey
I think we'll have to see some code...

Posted: Wed May 26, 2004 8:38 am
by Etherguy
Here is what I have

Code: Select all

<?php
$result1=MYSQL_QUERY("SELECT DISTINCT Username,sum(AcctSessionTime)/60  as SesTime From radacct where Username != '' group by Username");

while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) {
$usr[] = $line[Username];
$ses[] = $line[SesTime];
}
$chart[ 'chart_data' ] = array (array("",$usr) ,array( "",$ses)) ;

?>
The result is a never ending loop.

Let me know if there is anything else I can provide.

Posted: Wed May 26, 2004 11:37 am
by Etherguy
nothing on this.. huh...

Posted: Wed May 26, 2004 11:44 am
by magicrobotmonkey
change this:

Code: Select all

<?php
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
$usr[] = $line[Username]; 
$ses[] = $line[SesTime]; 
} 
?>
to this:

Code: Select all

<?php
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
$usr[] = $line['Username']; 
$ses[] = $line['SesTime']; 
} 
?>

Posted: Wed May 26, 2004 12:00 pm
by Etherguy
No go on that. Made no difference.. after the loop finally ends.. I see in my graph a title of Array... which tells me that it is not seeing the data, but rather the array it self. As I stated before I have tried imploding the array, and the data looks ok when echoed... but does not work when I try to make a multidimensional array out of it.

:(

Posted: Wed May 26, 2004 12:09 pm
by magicrobotmonkey
try:

Code: Select all

<?php
while ($line = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
print_r($line);
} 
?>
and see what that shows. Also try print_r($chart) once you fill it and check to see that its properly filled.

Posted: Wed May 26, 2004 1:25 pm
by Etherguy
The

Code: Select all

print_r($chart['chart_data']);
worked like a champ. I was able to see what I was missing in my array that was causing the looping issue.

Thank you for your time.

Posted: Wed May 26, 2004 1:27 pm
by magicrobotmonkey
yep - http://www.php.net is your friend!

Posted: Wed May 26, 2004 1:32 pm
by Etherguy
More then just a friend... more like a favorite uncle or something.

I am sure if php.net kept stats on visiting IP addresses mine would rank up there :D

Thanks again for the help!