Ye gods, how long have you been programming? You do know that PHP has define() and for loops, and that variables can be more than two characters long, don't you? That is some seriously messy code.
I'm still trying to sort out which bits are supposed to be 520 and which should be 523 and which should be 43 or 63 or whatever.
Okay, the problem. While you're using all these colours (170, 70, etc.) you never set them to any values; the only colour you do allocate ($background_color) is one you never use. So it's anyone's guess which one it actually will use if passed an index to a nonexistent palette entry (I think the last one allocated). When I rewrote the code (see below) the only change I made to the colour selection stuff was to use colours that have actually been allocated.
Okay, I rewrote some of it for my own sanity. It's still too ugly for my peace of mind. Selecting the bar colour is pretty arbitrary; if you've got something more appropriate then, well, you're the programmer 🙂
include ("./config/info.config");
$databaseserver = $host;
$databasename = $db_name; //the name of your database on the mysql server
$databaseuser = $db_user; //the name of the database-user
$databasepass = $db_pass; // the password to database
$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
if(!mysql_select_db("$databasename", $db))
{
die("DATABASE SELECTION FAILED");
}
header("Content-type: image/png");
$image_size = 600;
$graph_size = 500;
$im = imagecreate($image_size, $image_size) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0,0,0);
$lightgrey = imagecolorallocate($im, 192,192,192);
$palette = array(
imagecolorallocate($im, 106, 90, 205), // slateblue
imagecolorallocate($im, 139, 69, 19), // saddlebrown
imagecolorallocate($im, 255, 99, 71), // tomato
imagecolorallocate($im, 102, 205, 170), // mediumaquamarine
);
imagefill($im, 0,0,$background_color);
//background
$x_padding=23;
$y_padding=20;
$line_spacing = 20;
//imageline ($im, 0, 500, 500, 500, 50);
for($i=0; $i<$graph_size; $i+=$line_spacing)
{
imageline($im, $x_padding+$i, $y_padding, $x_padding+$i, $graph_size+$y_padding, $lightgrey);
imageline($im, $y_padding, $y_padding+$i, $graph_size+$x_padding, $y_padding+$i, $black);
}
//bars and text
$bar_width = 20;
$bar_padding = 20;
$bar_base=$graph_size+$y_padding;
$label_height=$bar_base+5;
$bars = mysql_query('select id,view from `tracking`');
$res_count = mysql_num_rows($res);
for($i=0; $i<$res_count; $i++)
{
$bar = mysql_fetch_assoc($bars);
$bar_height = ($graph_size+$y_padding - $bar["view"]);
$bar_left = $i*($bar_width+$bar_padding)+$x_padding;
$colour = $palette[$i%count($palette)];
imagefilledrectangle ($im, $bar_left, $bar_height, $bar_left+$bar_width, $bar_base, $colour);
imagestring($im, 3, $bar_left+3, $bar_height-15, $bar["view"], $black);
imagestring($im, 3, $bar_left, $label_height, $bar["id"], $black);
}
//side numbers
for($y=35; $y<=$graph_size+15; $y += 20)
{
$string = $graph_size+15-$y;
imagestring($im, 3, 0, $y, $string, $black);
}
imagepng($im);