runeveryday Posted August 18, 2011 Share Posted August 18, 2011 the database is right: DATABASE `poll`; TABLE `results` CREATE TABLE `results` ( book_type VARCHAR(50), num_votes INT ); INSERT INTO `results` values ('Classic', 15), ('Fantasy', 7), ('Humor', 32), ('Mystery', 12), ('Poetry', 25); the code: <?php $dbhandle = mysql_connect("localhost","root","123") or die("unable to connect to mysql"); $selected = mysql_select_db("poll",$dbhandle); $result = mysql_query("SELECT * FORM results"); $num_poller = mysql_num_rows($result); $total_votes = 0; while($row = mysql_fetch_array($result)){ $total_votes += $row{'num_votes'}; } mysql_data_seek($result,0); mysql_close($dbhandle); putenv('GDFONTPATH=C:\WINDOWS\Fonts'); $font = 'arial'; $y = 50; $width =700; $height = $num_poller * $bar_height *1.5 + 70; $bar_unit = ($width - 400)/100; $image = imagecreate($width,$height); $white = imagecolorallocate($image,255,255,255); $black = imagecolorallocate($image,0,0,0); $red = imagecolorallocate($image,255,0,0); $blue = imagecolorallocate($image,0,0,255); imagefill($image,$width,$height,$white); imagerectangle($image,0,0,$width-1,$height-1,$black); imagettftext($image,16,0,$width/3+50,$y-20,$black,$font,'poll results'); while($row = mysql_fetch_object($result)){ if($total_votes > 0){ $percent = intval(round(($row->num_votes/$total_votes)*100)); }else{ $percent =0; } imagettftext($image,12,0,10, $y+($bar_height/2), $black, $font, $row->book_type); //Output percentage for a particular value imagettftext($image, 12, 0, 170, $y + ($bar_height/2),$red,$font,$percent.'%'); $bar_length = $percent * $bar_unit; //Draw a shape that corresponds to 100% imagerectangle($image, $bar_length+221, $y-2, (220+(100*$bar_unit)), $y+$bar_height, $black); //Output a bar for a particular value imagefilledrectangle($image,220,$y-2,220+$bar_length, $y+$bar_height, $blue); //Output the number of votes imagettftext($image, 12, 0, 250+100*$bar_unit, $y+($bar_height/2), $black, $font, $row->num_votes.' votes cast.'); $y = $y + ($bar_height * 1.5); } header("Content-Type: image/jpeg"); imagejpeg($image); imagedestroy($image); it shows an error: the image...cannot be displayed because it contains errors?. what's wrong with the code? thank you Quote Link to comment https://forums.phpfreaks.com/topic/245088-whats-wrong-with-the-php-generate-image-bar-chart-code/ Share on other sites More sharing options...
voip03 Posted August 18, 2011 Share Posted August 18, 2011 What kind of ERRORS? Quote Link to comment https://forums.phpfreaks.com/topic/245088-whats-wrong-with-the-php-generate-image-bar-chart-code/#findComment-1258901 Share on other sites More sharing options...
PFMaBiSmAd Posted August 18, 2011 Share Posted August 18, 2011 You should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. You will save a ton of time. Your query is failing due to a typo in sql syntax, you have not defined the $bar_height variable, and you are likely getting an error concerning the font filename. You will need to browse directly to the page containing that code to see the php errors that are output after you set the error_reporting/display_errors settings and if output_buffering is on, you will need to turn that off as well. Quote Link to comment https://forums.phpfreaks.com/topic/245088-whats-wrong-with-the-php-generate-image-bar-chart-code/#findComment-1258902 Share on other sites More sharing options...
runeveryday Posted August 18, 2011 Author Share Posted August 18, 2011 the sql command is right, i added print_r($result); there are some output of it. may be the font path is wrong? but how to correct it. thank you. i added the $bar_height =20, the code still doesn't run. Quote Link to comment https://forums.phpfreaks.com/topic/245088-whats-wrong-with-the-php-generate-image-bar-chart-code/#findComment-1258903 Share on other sites More sharing options...
PFMaBiSmAd Posted August 18, 2011 Share Posted August 18, 2011 FROM is misspelled in your query. Whatever output you got from the print_r wasn't correct because $result isn't an array. Quote Link to comment https://forums.phpfreaks.com/topic/245088-whats-wrong-with-the-php-generate-image-bar-chart-code/#findComment-1258908 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.