Jump to content


Photo

php graph


  • Please log in to reply
24 replies to this topic

#21 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 September 2006 - 12:34 AM

To pull data from your table and use the bar.php graph method
<?php
$query = "SELECT jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
      FROM hits";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);

echo "<table>\n";
foreach ($data as $month => $hits) {
    echo "<tr>
            <td>$month</td>
            <td><img src='bar.php?val=$hits&max=1000'></td>
            <td>$hits</td>
            </tr>\n";
}
echo "</table>\n";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#22 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 September 2006 - 12:46 PM

Thank you barand.

This is a link so that you can crate a stats graph by month full instructions all thanks to barand.

http://freesingles.ath.cx/stats.inc



Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#23 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 September 2006 - 01:26 PM

In your database table, all those fields (except id) are hitcounts and therefore numeric. Change them from varchar() to INT.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#24 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 September 2006 - 01:35 PM

done cheers.

i love it your the best thank you.
barand can you kindly deatail this even more as i got it on a white board at home and trying to study it but it's really hard i also have you instuctions but that goes far but not as far as i need cheers.

ps only if you got time thank you.
<?php
// set dimensions
     $w = 300;
     $h = 12;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = $_GET['val'];
     $max = $_GET['max'];
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
	 if ($barw)
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#25 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 September 2006 - 06:13 PM

I added a few more explantory comments. Hope it helps.

<?php
// set dimensions
     $w = 300;
     $h = 12;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
         // Called by, for example : <img src='bar.php?val=25&max=100'>
         // This gets the val and max sent in the query string
     $val = $_GET['val'];
     $max = $_GET['max'];
// calculate dimensions of inner bar
         // Check that $max is not 0 as we need to divide by it
         // Calculate the width of bar to be plotted
         // As the full width of the bar represents a value of $max we
         //  need total width * ($val / $max)
         // Since there is a 1 pixel wide border all round, our total width
         //  for the value bar is the image width - 2
         // Floor() rounds it down to nearest whole number
         // Similarly, out bar height is image height - 2
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
         // Now draw the bar as a filled rectangle which fits inside the border
     if ($barw)
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users