PHP: Simple Chart Script

Thu, Jul 7, 2011 2-minute read

Source:

<?php
if (in_array($_GET["table_name"], array('cooler'))) {
	$table_name = $_GET["table_name"];
  }
else {
  $table_name = "cooler";
  }

if (in_array($_GET["order_value"], array('name', 'standard', 'overclock'))) {
	$order_value = $_GET["order_value"];
  }
else {
  $order_value = "name";
  }

if(in_array($_GET["order_align"], array('asc', 'desc'))) {
  $order_align = $_GET["order_align"];
  }
else {
  $order_align = "asc";
  }
?>

<html>
<head>
<title>PHP-Chart</title>
</head>
<body>

<?php
include("myConnectionDetails");
$db_link = @mysql_connect (MYSQL_HOST, MYSQL_BENUTZER, MYSQL_KENNWORT);
if ( ! $db_link )
{
  die('No connection available at the moment');
}

$db_sel = mysql_select_db( MYSQL_DATENBANK )
        or die("Database not available");
?>

<form name="form_table" action="php_chart.php"
 method="GET" enctype="text/html">
<p>Database</p>
<select name='table_name'>
  <option value='cooler'>CPU-Cooler</option>
</select>
<input type="Submit" value="Select" />
</form>

<?php
if ($table_name) {
	$ausgabe = mysql_query("select * FROM $table_name Order by $order_value $order_align");

	//Define arrays and variable i (for the while loop)
	$name = array();
	$standard = array();
	$overclock = array();
	$i = 0;

	while ($rows=mysql_fetch_array($ausgabe)) {

		//Arrays for the rows
		$name[$i] = $rows['name'];
		$standard[$i] = $rows['standard'];
		$overclock[$i] = $rows['overclock'];

		//Count up for each loop
		$i = $i+1;
	}

	// Define a new variable for the highest value of each array
	$max_standard = max($standard);
	$max_overclock = max($overclock);

	// Find out what value is the highest and calculate the lengh of the regarding bars
	for ($y = 0; $y < $i; $y++){
		if 	($max_standard > $max_overclock) {
			$standard_width[$y] = $standard[$y] / $max_standard * 100;
			$overclock_width[$y] = $standard[$y] / $max_overclock * 100;
		}
		else {
			$standard_width[$y] = $overclock[$y] / $max_standard * 100;
			$overclock_width[$y] = $overclock[$y] / $max_overclock * 100;
		}
	}

	//Now it's time to show the results
	echo "<table BORDER='2' CELLPADDING='2' CELLSPACING='2'>
	<th>Name
	<a href='php_chart.php?table_name=".$table_name."&order_value=name&order_align=asc'><img src='asc.png' /></a>
	<a href='php_chart.php?table_name=".$table_name."&order_value=name&order_align=desc'><img src='desc.png' /></a>
	</th>
	<th width='100px'>Chart</th>
	<th>Standard
	<a href='php_chart.php?table_name=".$table_name."&order_value=standard&order_align=asc'><img src='asc.png' /></a>
	<a href='php_chart.php?table_name=".$table_name."&order_value=standard&order_align=desc'><img src='desc.png' /></a>
	</th>
	<th>Overclock
	<a href='php_chart.php?table_name=".$table_name."&order_value=overclock&order_align=asc'><img src='asc.png' /></a>
	<a href='php_chart.php?table_name=".$table_name."&order_value=overclock&order_align=desc'><img src='desc.png' /></a>
	</th><tr>";
	// Put all values in the table
	for ($y = 0; $y < $i; $y++){
		echo "<th>".$name[$y]."</th>
		<th><div style='background-color:#297f03;width:".$standard_width[$y].";height:10px;'></div><div style='background-color:#c11007;width:".$overclock_width[$y].";height:10px;'></div></th>
		<th>".$standard[$y]."</th><th>".$overclock[$y]."</th><tr>
		";
	}
	echo "</table>";

}
?>
</body>
</html>