Gazz1982 Posted July 20, 2007 Share Posted July 20, 2007 I have created a graph using the below code. <?include("phpgraphlib.php"); $graph=new PHPGraphLib(400,300); $data=array("1"=>20, "2"=>198, "3"=>70, "4"=>90); $graph->addData($data); $graph->setTitle("Definitions"); $graph->setGradient("red", "maroon"); $graph->setBarOutlineColor("black"); $graph->setTextColor("blue"); $graph->createGraph(); // Closing connection mysql_close($link); ?> This is called from a html page using <img src="bar_graph.php" /> I have a database using mysql, how would i link this into the graph? ie from $data=array("1"=>20, "2"=>198, "3"=>70, "4"=>90); to $data=array("1"=$a, "2"=$b, "3"=>$c, "4"=>$d); in this case the $a/b/c and d would be called from a query I tried the above $data=array... but it didn't work any ideas? Gary Quote Link to comment Share on other sites More sharing options...
Barand Posted July 20, 2007 Share Posted July 20, 2007 I'm not clairvoyant so how are you getting $a, $b etc from the database? Are the cols from the same row or are you looping through records to get them? Quote Link to comment Share on other sites More sharing options...
Gazz1982 Posted July 20, 2007 Author Share Posted July 20, 2007 I'm connecting to a mysql database and conducting a query using: <?php // Connecting, selecting database $link = mysql_connect('127.0.0.1', 'root', '') or die('Could not connect: ' . mysql_error()); //echo 'Connected successfully'; mysql_select_db('gary') or die('Could not select database'); // Performing SQL query // Retrieve all the data from the "example" table $result = mysql_query("SELECT count(*) AS def1 FROM answer where def=1") or die(mysql_error()); while ($row = mysql_fetch_array ($result)) { echo"<table><tr><td>Definition 1 count=".$row["def1"]."</td></tr>\n"; } mysql_free_result ($result); this echos the result of the query then I need $a=".$row["def1"]." followed by the graph code Quote Link to comment Share on other sites More sharing options...
Barand Posted July 20, 2007 Share Posted July 20, 2007 Do you then repeat the process with ".. where def = 2" to get a value $b, etc etc Quote Link to comment Share on other sites More sharing options...
Gazz1982 Posted July 20, 2007 Author Share Posted July 20, 2007 yes, and def = 3 to get c and def = 4 to get d Quote Link to comment Share on other sites More sharing options...
Barand Posted July 20, 2007 Share Posted July 20, 2007 try <?php $sql = "SELECT def, count(*) AS def1 FROM answer GROUP BY def"; $result = mysql_query($sql); $data = array(); while (list($def, $def1) = mysql_fetch_row($result)) { $data[$def] = $def1; } ?> Quote Link to comment Share on other sites More sharing options...
Gazz1982 Posted July 20, 2007 Author Share Posted July 20, 2007 thank you this works great Quote Link to comment Share on other sites More sharing options...
Barand Posted July 20, 2007 Share Posted July 20, 2007 Don't forget to click "Topic solved" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.