Anyone, please help me here.:(

How can I draw a line graph using PHP with data from MySql database? Do you have sample code for this?

Thank you!

Recommended Answers

All 4 Replies

I personally use http://www.maani.us/xml_charts/

Basically you can make a connection via PHP to an SQL database. It then outputs as XML and the Flash object displays pretty, animated graphs in any form/shape you desire. It's free software, but you can purchase a license if you want to remove the clickable link of the graph back to their site.

It's highly customizable and you can display any graph you want, line, stackable, 3d graphs, pie etc..

Here's an example of the PHP/MySQL code you implement however you want for the use of this graph:

<?php

//connect to the database 
mysql_connect ( "host", "user", "password" );
mysql_select_db ( "Accounting" );

//start the XML output 
print "<chart>";
print "<chart_data>";

//output the first row that contains the years 
print "<row>";
print "<null/>";
$category = mysql_query ("SELECT Year FROM Growth GROUP BY Year ORDER BY Year");
for ( $column=0; $column < mysql_num_rows($category); $column++ ) {
	print "<string>".mysql_result ( $category, $column, "Year")."</string>";
}
print "</row>";

//output row 2 to 4. Each row contains a region name and its data 
$series = mysql_query ("SELECT Region FROM Growth GROUP BY Region ORDER BY Region");      
for ( $row=0; $row < mysql_num_rows($series); $row++ ) {
	print "<row>";
	$region = mysql_result ( $series, $row, "Region");
	print "<string>$region</string>";
	
	$data = mysql_query ("SELECT Revenue FROM Growth WHERE Region='$region' ORDER BY Year");      
	for ( $column=0; $column < mysql_num_rows($data); $column++ ) {
		print "<number>".mysql_result ( $data, $column, "Revenue")."</number>";
	}
	print "</row>";
}

//finish the XML output 
print "</chart_data>";
print "</chart>";

?>

do i still need to download the software to run this code?

i downloaded the chart.zip and the sample.html which is saved from it contains only texts, "This content requires JavaScript."

what will I do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.