Statistieken in grafiekweergave
Auteur: barry - 16 september 2004 - 01:08 - Gekeurd door: Dennisvb - Hits: 12042 - 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
<? mysql_connect("localhost","********","********");
mysql_select_db("********");
$ipadr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
$m_stat = mysql_query("SELECT COUNT(id) As aantal FROM `visits` WHERE ip = '$ipadr'") or die (mysql_error());
while($row = mysql_fetch_object($m_stat)){
$aantal = "$row->aantal";
$d = date("d");
$m = date("m");
$j = date("Y");
$u = date("h");
$min = date("i");
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()); } }
?>
<html>
<head>
<title>Statistieken [Dsound.nl / Barry Drooger]</title>
</head>
<body>
<select id="maand" onChange="top.location.href=options[selectedIndex].value">
<option selected value="#">-- Maand --</option>
<option value="index.php?m=01&j=2004">Januari 2004</option>
<option value="index.php?m=02&j=2004">Februari 2004</option>
<option value="index.php?m=03&j=2004">Maart 2004</option>
<option value="index.php?m=04&j=2004">April 2004</option>
<option value="index.php?m=05&j=2004">Mei 2004</option>
<option value="index.php?m=06&j=2004">Juni 2004</option>
<option value="index.php?m=07&j=2004">Juli 2004</option>
<option value="index.php?m=08&j=2004">Augustus 2004</option>
<option value="index.php?m=09&j=2004">September 2004</option>
<option value="index.php?m=10&j=2004">Oktober 2004</option>
<option value="index.php?m=11&j=2004">November 2004</option>
<option value="index.php?m=12&j=2004">December 2004</option>
</select>
<hr>
<img src="stats.php?jaar=2004<? if($_GET['m']==""){ } else { echo "&maand=" . $_GET['m']; } ?>" border="0" id="stats">
</body>
</html>
$ipadr = $HTTP_SERVER_VARS["REMOTE_ADDR"]; $aantal = "$row->aantal"; 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()); } } ?> <html> <head> <title>Statistieken [Dsound.nl / Barry Drooger]</title> </head> <body> <select id="maand" onChange="top.location.href=options[selectedIndex].value"> <option selected value="#">-- Maand --</option> <option value="index.php?m=01&j=2004">Januari 2004</option> <option value="index.php?m=02&j=2004">Februari 2004</option> <option value="index.php?m=03&j=2004">Maart 2004</option> <option value="index.php?m=04&j=2004">April 2004</option> <option value="index.php?m=05&j=2004">Mei 2004</option> <option value="index.php?m=06&j=2004">Juni 2004</option> <option value="index.php?m=07&j=2004">Juli 2004</option> <option value="index.php?m=08&j=2004">Augustus 2004</option> <option value="index.php?m=09&j=2004">September 2004</option> <option value="index.php?m=10&j=2004">Oktober 2004</option> <option value="index.php?m=11&j=2004">November 2004</option> <option value="index.php?m=12&j=2004">December 2004</option> </select> <hr> <img src="stats.php?jaar=2004 <? if($_GET['m']==""){ } else { echo "&maand=" . $_GET['m']; } ?>" border="0" id="stats"> </body> </html>
stats.php
<?
mysql_connect("localhost","********","********");
mysql_select_db("********");
$maand = $_GET['maand'];
$jaar = $_GET['jaar'];
$today_month = $maand;
if($today_month=="01"){ $maxday = 31; }
elseif($today_month=="02"){ $maxday = 28; }
elseif($today_month=="03"){ $maxday = 31; }
elseif($today_month=="04"){ $maxday = 30; }
elseif($today_month=="05"){ $maxday = 31; }
elseif($today_month=="06"){ $maxday = 30; }
elseif($today_month=="07"){ $maxday = 31; }
elseif($today_month=="08"){ $maxday = 31; }
elseif($today_month=="09"){ $maxday = 30; }
elseif($today_month=="10"){ $maxday = 31; }
elseif($today_month=="11"){ $maxday = 30; }
else { $maxday = 31; }
if($maand=="" || $jaar==""){ $width = 256; $height = 32; } else { $width = 800; $height = 384; }
$maxheight = $height - 20;
$grid = 15;
$aantal = $maxday;
$type = $_GET['type'];
if($type=="uniek"){ $sqlunique = "AND `type` = 'uniek'"; } else { $sqlunique = ""; }
for($a=1;$a<=$aantal;$a++){
if($a<=9) { $dag = "0" . $a; } else { $dag = $a; }
$mysql = mysql_query("SELECT * FROM `visits` WHERE dag = '$dag' AND jaar = '$jaar' AND maand = '$maand' $sqlunique") or die (mysql_error());
$hits = mysql_num_rows($mysql);
$statsday[$a] = $hits;
}
$totalstatsday = array_sum($statsday);
if($totalstatsday=="0"){
$width = 352; $height = 32;
$img = imagecreate($width, $height);
$bgcolor = imagecolorallocate($img,255,255,255);
$pcolor = imagecolorallocate($img,1,215,127);
$pcolor_u = imagecolorallocate($img,255,0,0);
$gridcolor = imagecolorallocate($img,191,191,223);
$txtcolor = imagecolorallocate($img,0,0,0);
imagestring($img,4, 16, 8, "Geen resultaten voor maand: $maand jaar: $jaar",$txtcolor);
imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor);
} else {
$img = imagecreate($width, $height);
$bgcolor = imagecolorallocate($img,255,255,255);
$pcolor = imagecolorallocate($img,1,215,127);
$pcolor_u = imagecolorallocate($img,255,0,0);
$gridcolor = imagecolorallocate($img,191,191,223);
$txtcolor = imagecolorallocate($img,0,0,0);
// Hieronder de code voor het maken van de grafiek... In principe hoeft hieronder niets veranderd te worden...
imagefilledrectangle($img, 0, 0, $width, $height,$bgcolor);
$gstep = ($width-30) / $aantal;
if($maand=="" || $jaar==""){ } else { //
if ($grid > 0) {
for ($v=30;$v<=$width;$v=$v+$gstep) {
imageline($img, $v, 0, $v, $maxheight, $gridcolor);
}
for ($h=0;$h<=$maxheight;$h=$h+$grid) {
imageline($img, 30, $h, $width, $h, $gridcolor);
}
}
$step = ($width-30) /$aantal;
$x = 30 - $gstep;
// Het aantal stappen verticaal berekenen
$max_pos = 0;
for ($i=0;$i<count($statsday);$i++){
if ($max_pos < $statsday[$i]) {
$max_pos = $statsday[$i];
}
} //
$max_value = $max_pos / $maxheight;
$polygon = array($x,$maxheight);
for ($a=0;$a<=count($statsday);$a++){
$y = $maxheight - ($statsday[$a]/$max_value);
$polygon[] = $x;
$polygon[] = $y;
$x = $x + $step;
}
$x = $x - $step;
$polygon[] = $x;
$polygon[] = $maxheight;
// De grafiek tekenen:
if($maand==""){ imagestring($img,4, 16, 8, "Voer een maand in \"?maand=xx\"",$txtcolor);
imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); }
elseif($jaar=="") { imagestring($img,4, 16, 8, "Voer een jaar in \"?jaar=xx\"",$txtcolor);
imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); } else {
imagepolygon($img,$polygon,(count($polygon)/2),$pcolor); }
if($maand=="" || $jaar==""){ } else {/**/imagefilledrectangle($img, 0, 0, 30, $height, $bgcolor);
imageline($img, 30, 0, 30, $maxheight, $txtcolor);
imageline($img, 30, $maxheight, $width, $maxheight, $txtcolor);
$pos = 30; // Horizontale nummering
for ($h=1;$h<=$aantal;$h++) {
imagestring($img,1,$pos, $maxheight+5, $h,$txtcolor);
$pos = $pos + $step;
}
$step = $max_value; // Verticale nummering
for ($v=9;$v<=($height-30);$v=$v=$v+15) {
$pos = floor((($height-30) - $v) * $step);
imagestring($img,1,12, $v, $pos,$txtcolor);
}
imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor);
}/**/ } }
Header("Content-type: image/png");
ImagePng($img);
ImageDestroy($img);
?>
<? $maand = $_GET['maand']; $jaar = $_GET['jaar']; $today_month = $maand; if($today_month=="01"){ $maxday = 31; } elseif($today_month=="02"){ $maxday = 28; } elseif($today_month=="03"){ $maxday = 31; } elseif($today_month=="04"){ $maxday = 30; } elseif($today_month=="05"){ $maxday = 31; } elseif($today_month=="06"){ $maxday = 30; } elseif($today_month=="07"){ $maxday = 31; } elseif($today_month=="08"){ $maxday = 31; } elseif($today_month=="09"){ $maxday = 30; } elseif($today_month=="10"){ $maxday = 31; } elseif($today_month=="11"){ $maxday = 30; } else { $maxday = 31; } if($maand=="" || $jaar==""){ $width = 256; $height = 32; } else { $width = 800; $height = 384; } $maxheight = $height - 20; $grid = 15; $aantal = $maxday; $type = $_GET['type']; if($type=="uniek"){ $sqlunique = "AND `type` = 'uniek'"; } else { $sqlunique = ""; } for($a=1;$a<=$aantal;$a++){ if($a<=9) { $dag = "0" . $a; } else { $dag = $a; } $mysql = mysql_query("SELECT * FROM `visits` WHERE dag = '$dag' AND jaar = '$jaar' AND maand = '$maand' $sqlunique") or die (mysql_error()); $statsday[$a] = $hits; } if($totalstatsday=="0"){ $width = 352; $height = 32; $img = imagecreate($width, $height); $bgcolor = imagecolorallocate($img,255,255,255); $pcolor = imagecolorallocate($img,1,215,127); $pcolor_u = imagecolorallocate($img,255,0,0); $gridcolor = imagecolorallocate($img,191,191,223); $txtcolor = imagecolorallocate($img,0,0,0); imagestring($img,4, 16, 8, "Geen resultaten voor maand: $maand jaar: $jaar",$txtcolor); imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); } else { $img = imagecreate($width, $height); $bgcolor = imagecolorallocate($img,255,255,255); $pcolor = imagecolorallocate($img,1,215,127); $pcolor_u = imagecolorallocate($img,255,0,0); $gridcolor = imagecolorallocate($img,191,191,223); $txtcolor = imagecolorallocate($img,0,0,0); // Hieronder de code voor het maken van de grafiek... In principe hoeft hieronder niets veranderd te worden... imagefilledrectangle($img, 0, 0, $width, $height,$bgcolor); $gstep = ($width-30) / $aantal; if($maand=="" || $jaar==""){ } else { // if ($grid > 0) { for ($v=30;$v<=$width;$v=$v+$gstep) { imageline($img, $v, 0, $v, $maxheight, $gridcolor); } for ($h=0;$h<=$maxheight;$h=$h+$grid) { imageline($img, 30, $h, $width, $h, $gridcolor); } } $step = ($width-30) /$aantal; $x = 30 - $gstep; // Het aantal stappen verticaal berekenen $max_pos = 0; for ($i=0;$i<count($statsday);$i++){ if ($max_pos < $statsday[$i]) { $max_pos = $statsday[$i]; } } // $max_value = $max_pos / $maxheight; $polygon = array($x,$maxheight); for ($a=0;$a<=count($statsday);$a++){ $y = $maxheight - ($statsday[$a]/$max_value); $polygon[] = $x; $polygon[] = $y; $x = $x + $step; } $x = $x - $step; $polygon[] = $x; $polygon[] = $maxheight; // De grafiek tekenen: if($maand==""){ imagestring($img,4, 16, 8, "Voer een maand in \"?maand=xx\"",$txtcolor); imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); } elseif($jaar=="") { imagestring($img,4, 16, 8, "Voer een jaar in \"?jaar=xx\"",$txtcolor); imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); } else { imagepolygon ($img,$polygon,(count($polygon)/2),$pcolor); } if($maand=="" || $jaar==""){ } else {/**/imagefilledrectangle($img, 0, 0, 30, $height, $bgcolor); imageline($img, 30, 0, 30, $maxheight, $txtcolor); imageline($img, 30, $maxheight, $width, $maxheight, $txtcolor); $pos = 30; // Horizontale nummering for ($h=1;$h<=$aantal;$h++) { imagestring($img,1,$pos, $maxheight+5, $h,$txtcolor); $pos = $pos + $step; } $step = $max_value; // Verticale nummering for ($v=9;$v<=($height-30);$v=$v=$v+15) { $pos = floor((($height-30) - $v) * $step); imagestring($img,1,12, $v, $pos,$txtcolor); } imagerectangle($img, 0, 0, ($width-1), ($height-1), $txtcolor); }/**/ } } Header("Content-type: image/png"); ImagePng($img); ImageDestroy($img); ?>
Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|