login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Statistieken > Statistieken in grafiekweergave

Statistieken in grafiekweergave

Auteur: barry - 16 september 2004 - 01:08 - Gekeurd door: Dennisvb - Hits: 11966 - Aantal punten: 4.32 (14 stemmen)



Dit script laat de statistieken zien van je website in een grafiek.
Uiteraard de connecties even aanpassen in de scripts (sterretjes vervangen).
Het voorbeeld staat op een website die ik onder mijn beheer heb (en nog niet af is...)

Voor dit script heb je een tabel nodig en 2 bestanden:

Tabel:

CREATE TABLE `visits` (
`id` int(20) NOT NULL auto_increment,
`ip` varchar(50) NOT NULL default '',
`dag` char(3) NOT NULL default '',
`maand` char(2) NOT NULL default '',
`jaar` varchar(4) NOT NULL default '',
`uur` char(2) NOT NULL default '',
`minuten` char(2) NOT NULL default '',
`type` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `ip` (`ip`,`dag`,`maand`,`jaar`,`uur`,`minuten`),
KEY `type` (`type`)
) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=1 ;

Pagina's:
index.php
stats.php

Code:
Index.php

  1. <? mysql_connect("localhost","********","********");
  2. mysql_select_db("********");
  3. $ipadr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  4. $m_stat = mysql_query("SELECT COUNT(id) As aantal FROM `visits` WHERE ip = '$ipadr'") or die (mysql_error());
  5. while($row = mysql_fetch_object($m_stat)){
  6. $aantal = "$row->aantal";
  7. $d = date("d");
  8. $m = date("m");
  9. $j = date("Y");
  10. $u = date("h");
  11. $min = date("i");
  12. if($aantal > 0){ mysql_query("INSERT INTO `visits` ( `id` , `ip` , `dag` , `maand` , `jaar` , `uur` , `minuten`, `type` ) VALUES ('', '$ipadr', '$d', '$m', '$j', '$u', '$min', 'klik');") or die (mysql_error()); } else { mysql_query("INSERT INTO `visits` ( `id` , `ip` , `dag` , `maand` , `jaar` , `uur` , `minuten`, `type` ) VALUES ('', '$ipadr', '$d', '$m', '$j', '$u', '$min', 'uniek');") or die (mysql_error()); } }
  13. ?>
  14. <html>
  15. <head>
  16. <title>Statistieken [Dsound.nl / Barry Drooger]</title>
  17. </head>
  18. <body>
  19. <select id="maand" onChange="top.location.href=options[selectedIndex].value">
  20. <option selected value="#">-- Maand --</option>
  21. <option value="index.php?m=01&j=2004">Januari 2004</option>
  22. <option value="index.php?m=02&j=2004">Februari 2004</option>
  23. <option value="index.php?m=03&j=2004">Maart 2004</option>
  24. <option value="index.php?m=04&j=2004">April 2004</option>
  25. <option value="index.php?m=05&j=2004">Mei 2004</option>
  26. <option value="index.php?m=06&j=2004">Juni 2004</option>
  27. <option value="index.php?m=07&j=2004">Juli 2004</option>
  28. <option value="index.php?m=08&j=2004">Augustus 2004</option>
  29. <option value="index.php?m=09&j=2004">September 2004</option>
  30. <option value="index.php?m=10&j=2004">Oktober 2004</option>
  31. <option value="index.php?m=11&j=2004">November 2004</option>
  32. <option value="index.php?m=12&j=2004">December 2004</option>
  33. </select>
  34. <hr>
  35. <img src="stats.php?jaar=2004<? if($_GET['m']==""){ } else { echo "&maand=" . $_GET['m']; } ?>" border="0" id="stats">
  36. </body>
  37. </html>




stats.php

  1. <?
  2. mysql_connect("localhost","********","********");
  3. mysql_select_db("********");
  4. $maand = $_GET['maand'];
  5. $jaar = $_GET['jaar'];
  6.  
  7. $today_month = $maand;
  8. if($today_month=="01"){ $maxday = 31; }
  9. elseif($today_month=="02"){ $maxday = 28; }
  10. elseif($today_month=="03"){ $maxday = 31; }
  11. elseif($today_month=="04"){ $maxday = 30; }
  12. elseif($today_month=="05"){ $maxday = 31; }
  13. elseif($today_month=="06"){ $maxday = 30; }
  14. elseif($today_month=="07"){ $maxday = 31; }
  15. elseif($today_month=="08"){ $maxday = 31; }
  16. elseif($today_month=="09"){ $maxday = 30; }
  17. elseif($today_month=="10"){ $maxday = 31; }
  18. elseif($today_month=="11"){ $maxday = 30; }
  19. else { $maxday = 31; }
  20.  
  21. if($maand=="" || $jaar==""){ $width = 256; $height = 32; } else { $width = 800; $height = 384; }
  22. $maxheight = $height - 20;
  23. $grid = 15;
  24. $aantal = $maxday;
  25.  
  26.  
  27. $type = $_GET['type'];
  28. if($type=="uniek"){ $sqlunique = "AND `type` = 'uniek'"; } else { $sqlunique = ""; }
  29.  
  30.  
  31. for($a=1;$a<=$aantal;$a++){
  32. if($a<=9) { $dag = "0" . $a; } else { $dag = $a; }
  33. $mysql = mysql_query("SELECT * FROM `visits` WHERE dag = '$dag' AND jaar = '$jaar' AND maand = '$maand' $sqlunique") or die (mysql_error());
  34. $hits = mysql_num_rows($mysql);
  35. $statsday[$a] = $hits;
  36. }
  37. $totalstatsday = array_sum($statsday);
  38. if($totalstatsday=="0"){
  39. $width = 352; $height = 32;
  40. $img = imagecreate($width, $height);
  41. $bgcolor = imagecolorallocate($img,255,255,255);
  42. $pcolor = imagecolorallocate($img,1,215,127);
  43. $pcolor_u = imagecolorallocate($img,255,0,0);
  44. $gridcolor = imagecolorallocate($img,191,191,223);
  45. $txtcolor = imagecolorallocate($img,0,0,0);
  46. imagestring($img,4, 16, 8, "Geen resultaten voor maand: $maand jaar: $jaar",$txtcolor);
  47. imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor);
  48. } else {
  49. $img = imagecreate($width, $height);
  50. $bgcolor = imagecolorallocate($img,255,255,255);
  51. $pcolor = imagecolorallocate($img,1,215,127);
  52. $pcolor_u = imagecolorallocate($img,255,0,0);
  53. $gridcolor = imagecolorallocate($img,191,191,223);
  54. $txtcolor = imagecolorallocate($img,0,0,0);
  55.  
  56.  
  57. // Hieronder de code voor het maken van de grafiek... In principe hoeft hieronder niets veranderd te worden...
  58.  
  59.  
  60. imagefilledrectangle($img, 0, 0, $width, $height,$bgcolor);
  61.  
  62. $gstep = ($width-30) / $aantal;
  63. if($maand=="" || $jaar==""){ } else { //
  64. if ($grid > 0) {
  65. for ($v=30;$v<=$width;$v=$v+$gstep) {
  66. imageline($img, $v, 0, $v, $maxheight, $gridcolor);
  67. }
  68. for ($h=0;$h<=$maxheight;$h=$h+$grid) {
  69. imageline($img, 30, $h, $width, $h, $gridcolor);
  70. }
  71. }
  72.  
  73. $step = ($width-30) /$aantal;
  74. $x = 30 - $gstep;
  75.  
  76. // Het aantal stappen verticaal berekenen
  77. $max_pos = 0;
  78. for ($i=0;$i<count($statsday);$i++){
  79. if ($max_pos < $statsday[$i]) {
  80. $max_pos = $statsday[$i];
  81. }
  82.  
  83. } //
  84.  
  85.  
  86. $max_value = $max_pos / $maxheight;
  87.  
  88. $polygon = array($x,$maxheight);
  89. for ($a=0;$a<=count($statsday);$a++){
  90. $y = $maxheight - ($statsday[$a]/$max_value);
  91. $polygon[] = $x;
  92. $polygon[] = $y;
  93. $x = $x + $step;
  94. }
  95. $x = $x - $step;
  96. $polygon[] = $x;
  97. $polygon[] = $maxheight;
  98.  
  99. // De grafiek tekenen:
  100. if($maand==""){ imagestring($img,4, 16, 8, "Voer een maand in \"?maand=xx\"",$txtcolor);
  101. imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); }
  102. elseif($jaar=="") { imagestring($img,4, 16, 8, "Voer een jaar in \"?jaar=xx\"",$txtcolor);
  103. imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); } else {
  104. imagepolygon($img,$polygon,(count($polygon)/2),$pcolor); }
  105.  
  106.  
  107. if($maand=="" || $jaar==""){ } else {/**/imagefilledrectangle($img, 0, 0, 30, $height, $bgcolor);
  108. imageline($img, 30, 0, 30, $maxheight, $txtcolor);
  109. imageline($img, 30, $maxheight, $width, $maxheight, $txtcolor);
  110. $pos = 30; // Horizontale nummering
  111. for ($h=1;$h<=$aantal;$h++) {
  112.   imagestring($img,1,$pos, $maxheight+5, $h,$txtcolor);
  113.   $pos = $pos + $step;
  114. }
  115. $step = $max_value; // Verticale nummering
  116. for ($v=9;$v<=($height-30);$v=$v=$v+15) {
  117.   $pos = floor((($height-30) - $v) * $step);
  118.   imagestring($img,1,12, $v, $pos,$txtcolor);
  119.  
  120. }
  121. imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor);
  122. }/**/ } }
  123.  
  124. Header("Content-type: image/png");
  125. ImagePng($img);
  126. ImageDestroy($img);
  127. ?>
Download code! Download code (.txt)

Download dit script! Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (8)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.044s