Scripts

Bezoekersteller met image

Dit is een class met een aantal functie's om een mooi bezoekers grafiek te kunnen maken. Roep het aan door bijvoorbeeld: $teller= new teller(); $teller->connect('test', 'blaat', 'localhost', 'bezoekers_db');//gegevens om met de database te connecten test = username. blaat = wachtwoord. localhost = host adres. bezoekers_db = database naam. $teller->check_table(); om een tabel aan te maken in je database $teller->user_add(); om een bezoeker toe te voegen in de database hij wordt maar 1x toegevoegd zolang zijn sessie blijft bestaan. $teller->picture('2', '0', '500', '400', 'EGEGEG', '000000', 'FFFFFF' ); om het grafiekje te voorschijn te toveren. 2 betekend 2 maanden verder dan we nu zijn. 0 betekend 0 jaren verder dan we nu zijn. 500 pixels in de x richting. 400 pixels in de y richting. EGEGEG is de kleur van de achtergrond (grijs) 000000 is de kleur van de index lijn (zwart) FFFFFF is de kleur van de lijn die het aantal bezoekers aangeeft (wit) Ik hoop dat jullie er wat aan hebben. Ik heb nog niet de tijd gehad om het te kunnen testen, me database was wat te leeg. Veel plezier ermee!

bezoekersteller-met-image
<?php
session_start();
ob_start();

################################################################################
#
#	This file contains al the functions
#              Marcel Boersma
#
################################################################################

	class teller{
        function connect($username, $password, $host, $db){//function connect to db
                mysql_connect($host, $username, $password) OR die("There was no connection to the db");//connect to the host
                mysql_select_db($db) OR die("Couldn't connect to database....");//connect to the database
        }

		function check_table(){//open function
			//get table list
			$tables=mysql_query("SHOW TABLES");//get tables
			$counted=mysql_num_rows($tables);//count tables
			if($counted != 0){//check if counted isn't zero
				for($x=0;$x<=$counted-1;$x++){//for each result load result into array
					$table[$x]=mysql_result($tables,$x);//load in array
				}
			}
	
			$table_array="teller";//table name
			if(!in_array($table_array,$table)){//check if exists
				$this->create_table("teller");
			}
		}//close function



	private function create_table($table_name){
			switch($table_name){//switch table name
				case 'teller' :	$table_data="CREATE TABLE `teller` (
							     `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
							     `ip` VARCHAR( 32 ) NOT NULL ,
							     `date` INT NOT NULL 
							     ) TYPE = MYISAM ;";//create data
						mysql_query($table_data);//create table
						$this->check_table;//check again
				break;
			}

		}

		function user_add(){
			$ip=$_SERVER['REMOTE_ADDR'];//get ip address
			if(!isset($_SESSION['ip'])){//check if session exsists
				$_SESSION['ip']=$ip;//make session
				$date=$this->make_date();//make date
				mysql_query("INSERT INTO teller (`ip`, `date`) VALUES ('$ip', '$date')");//insert into the database

			}
		}

		function make_date(){
			//set date
			$month=date("n");//set month
			$day=date("j");//set day
			$year=date("Y");//set year

			$date_now=mktime(0, 0, 0, $month, $day, $year);//make timestamp


		return $date_now;//return timestamp
		}

        function color($color, $index){
            if(strlen($color) != 6){//if color hasn't 6 chars, color is white
                $color='FFFFFF';
            }
            switch($index){//switch index
                case '1' : $splited=substr($color, 0, 2);// index = 1 return the first heximals
                break;
                case '2' : $splited=substr($color, 2, 4);//index = 2 return the second heximals
                break;
                case '3' : $splited=substr($color, 4, 6);//index = 3 return the third heximals
                break;
            }
            $color=hexdec($splited);//get heximals
            return $color;//return heximals
        }

        function picture($month, $year, $x, $y, $color_background, $color_line, $line){
            //background colors
            $color_background1=$this->color($color_background, 1);
            $color_background2=$this->color($color_background, 2);
            $color_background3=$this->color($color_background, 3);
            
            //indexline colors
            $color_line1=$this->color($color_line, 1);
            $color_line2=$this->color($color_line, 2);
            $color_line3=$this->color($color_line, 3);
            
            //line color
            $line1=$this->color($line, 1);
            $line2=$this->color($line, 2);
            $line3=$this->color($line, 3);


			header("Content-type: image/png");//set header
			$im = @imagecreate($x, $y)//create image
   			

			$background=imagecolorallocate($im,$color_background1 , $color_background2, $color_background3);//set background
			$indexline=imagecolorallocate($im, $color_line1, $color_line2, $color_line3);//set indexline color
			$line=imagecolorallocate($im, $line1, $line2, $line3);//set line color

			$numbersofindexline=$y/20;//count index lines
			for($a=0;$a<$numbersofindexline;$a++){
				$ya=$a*20;//heigth of index line
				imageline($im, '0', $ya, '500', $ya, $indexline);//$im=image, x1, y1, x2, y2, color
				imagestring($im, 100, 5, $ya-2,  $y-$ya, $indexline);//index number
			}
            //load visitors out of db for this month
                switch($month){//switch month type
                case '' : $plus=0;//if type is empty mont=0
                break;
                default : if(ctype_digit($month) AND $month <= 12){ $month=$month; }else{ $month=0; }//if type isn't empty and is a number plus is okay, else month = 0
                break;

            }
                 //print month name
                $monthname=date("F");
                imagestring($im, 35, $x-50, $y-($y-25), $monthname, $line);


            $visitors=array();//make array for visitors
            $counted_days_in_month=date('t');//count days in month
            for($a=0 ; $a <= $counted_days_in_month; $a++){
                    $date=mktime(0, 0, 0, date('n')+$month, date('j')+$a , date('Y')+$year);//begin time
                    $qry_count=mysql_query("SELECT * FROM `teller` WHERE date='$date'");//count for each day visitors
                    $counted=mysql_num_rows($qry_count);
                    $visitors[$a]=$counted;//put visitors into array
            }
            
            $inarray=$counted; //values in array is simulair to the counted days
			for($i=0;$i<$inarray-1;$i++){
				$x_1=$i*($x/$inarray);//x divide counted days
				$y_1=$y-$visitors[$i];//counted visitors day one
				$x_2=$x_1+($x/$inarray);//draw line to
				$y_2=$y-$visitors[$i+1];//counted visitors next day
				
				imageline($im, $x_1, $y_1, $x_2, $y_2, $line);//dray line
				imagestring($im, 100, $x_2, $y_2, $visitors[$i+1], $line);//set numbers

			}
			imagepng($im);
			imagedestroy($im);

		}

	}


?>

Reacties

0
Nog geen reacties.