Jump to content

Drawing Bar Graphs using GD/MySQL


coool
Go to solution Solved by coool,

Recommended Posts

Hi..

 

I'm having problem in this code !

 

I beileve it's almost done, but just need a little fix !

 

here's the code:

image.php

<?php 
//already been connected to database 

$sql = "SELECT Status FROM Table1 GROUP BY Status"; 
$x = mysql_query($sql) or die("Error - Could not $sql" . mysql_error()); 

$sql = "SELECT COUNT(Items) FROM Table1 GROUP BY Status"; 
$y = mysql_query($sql) or die("Error - Could not $sql" . mysql_error()); 

//x= sold,shipped,InStock,UnKnown 
//y= 8,6,3,5 

        $width = 300; 
        $height = 200; 

        header ("Content-type: image/png"); 

        $image = ImageCreate($width,$height) or die ("Cannot Create image"); 

        $white = ImageColorAllocate($image,255,255,255); 
        $black = ImageColorAllocate($image,0,0,0); 
        $red = ImageColorAllocate($image,255,0,0); 
        $green = ImageColorAllocate($image,0,255,0); 
        $blue = ImageColorAllocate($image,0,0,255); 

        $maxY = 0; 
        for ($i=0;$i<7;$i++){ 
          if ($y[$i] > $maxY)$maxY=$y[$i]; 
        } 

        ImageRectangle($image,1,1,319,239,$black); 

        imageLine($image,10,5,10,230,$blue); 
        imageLine($image,10,230,300,230,$blue); 

        ImageString($im,3,15,5,"CR ID",$black); 
        ImageString($im,3,280,240,"Status",$black); 
        ImageString($im,5,100,50,"Simple Graph",$red); 
        ImageString($im,5,125,75,"by Lina",$green); 

        $xPos = 15; 
        $yPos = 230; 
        $barWidth = 20; 
        $barHeight = 0; 

        for ($i=0;$i<3;$i++){ 
          $barHeight = ($y[$i]/$maxY)* 100; 
          imagerectangle($image,$xPos,$yPos,$xPos+$barWidth,($yPos-$barHeight),$red); 
          imagestring( $image,2,$xPos-1,$yPos+1,$x[$i],$black); 
          imagestring( $image,2,$xPos-1,$yPos+10,$y[$i],$black); 
          $xPos += ($barWidth+20); 
        } 

        ImagePng($image, "graph.png", 90); 
?> 

 

page.php

 
.... 
<img src="image.php"/> 
.... 

 

Error: after excuting page.php

The image “http://websiteName.com/image.php” cannot be displayed, because it contains errors.

Link to comment
Share on other sites

As deadlimp said, if you want to display it dynamically with an img tag, just

 

imagepng($image);

imagedestroy($image);      // remove image from memory.

 

Also, you've queried for x and y but haven't got their values from the query results, and you can get x andy vals with 1 query

 

<?php
$sql = "SELECT Status, COUNT(*) FROM Table1 GROUP BY Status"; 
$res = mysql_query($sql) or die("Error - Could not $sql" . mysql_error()); 
while (list ($xval, $yval) = mysql_fetch_row($res))
{
        $x[] = $xval;
        $y[] = $yval;
}

$maxY = max($y);       // use max() to get the max value
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.