Location: PHPKode > scripts > Counter Class with graphics > counter-class-with-graphics/counter.class.php
<?php

// webcounter requires the user_block class: download @ http://www.easywe.com
// author: Daniel Eder <hide@address.com>
// implements ADODB library
// if you dont know how to implement this class with all the features we have an easy web installer at http://www.easywe.com
// the counter styles/images are downloadable here: http://www.easywe.com/website/code/counter/images.zip

/*
CREATE TABLE easywe_counter (
  ID int(11) NOT NULL auto_increment,
  path varchar(255) NOT NULL default '',
  hitcount int(11) NOT NULL default '0',
  style varchar(255) NOT NULL default '',
  PRIMARY KEY  (ID)
) TYPE=MyISAM;
*/

include('adodb/adodb.inc.php');
define("dbhost","localhost");
define("dbuser","username");
define("dbpasswd","password");
define("dbname","databasename");


function EASY_NEW_DB(&$dbs,$host="",$user="",$pw="",$db=""){
    $host = ((!empty($host))?$host:dbhost);
	$user = ((!empty($user))?$user:dbuser);
	$pw = ((!empty($pw))?$pw:dbpasswd);
	$db = ((!empty($db))?$db:dbname);
    $dbs = ADONewConnection ("mysql");
 	if(!$dbs->Connect ($host, $user, $pw, $db)){
		//pr($dbs);
		return $dbs->ErrorNo();
	}
}

class EASY_Counter{
	var $db;
	var $EASY_tableprefix = "easywe_";
    var $type = "counter";
	var $EASYWE_includepath = "";

	function EASY_Counter(){
		EASY_NEW_DB($this->db);	
	}
	
	function mainCount($path, $style, $enableblocking){
		if($enableblocking == "yes"){
			$countNumber = $this->getCountNumberBlocking($path);
		}else{
			$countNumber = $this->getCountNumber($path);
		}

		$this->getgifs($countNumber,$style);
	}

	function getCountNumber($val){

		

		If(!is_int($val)&&(!empty($val))){
		
			$path=$val;
			$rs = $this->db->Execute("SELECT path, ID, hitcount FROM ".$EASY_tableprefix."counter WHERE path='$path'");
					if ($rs === false) die("DATABASE CONNECT TO ".$EASY_tableprefix."counter FAILED");
			if($rs->EOF){
				$this->db->Execute("INSERT INTO ".$EASY_tableprefix."counter (path, hitcount) VALUES('".trim($path)."',1)");
				return (int) ($rs->fields['hitcount']);
			}else{
				$this->db->Execute("UPDATE ".$EASY_tableprefix."counter SET hitcount=hitcount+1 WHERE path='$path'");
				return (int) ($rs->fields['hitcount']+1);
			}

		}elseif(is_int($val)&&(!empty($val))){
		
			$path=$val;
			$rs = $this->db->Execute("SELECT path, ID, hitcount FROM ".$EASY_tableprefix."counter WHERE ID=$path");
				if ($rs === false) die("DATABASE CONNECT TO ".$EASY_tableprefix."counter FAILED");
			if($rs->EOF){
				Echo "This counter ID does not exist. You must create one or choose a path.";
			}else{
				$this->db->Execute("UPDATE ".$EASY_tableprefix."counter SET hitcount=hitcount+1 WHERE ID=$path");
				return (int) ($rs->fields['hitcount']+1);			
			}
		}else{
		     echo "Enter Path or ID Parameter";
		}
		Return;
		
	}

	function getCountNumberBlocking($val){

		$block_easycounter = new EASY_ipblock();
		$user_visited = $block_easycounter->user_block($GLOBALS["REMOTE_ADDR"], $GLOBALS["type"], $val, 30);


		$path=$val;
		$rs = $this->db->Execute("SELECT path, ID, hitcount FROM ".$EASY_tableprefix."counter WHERE path='$path'");
			if ($rs === false) die("DATABASE CONNECT TO ".$EASY_tableprefix."counter FAILED");
		If(!is_int($val)&&(!empty($val))){
		
			if($rs->EOF){
				$this->db->Execute("INSERT INTO ".$EASY_tableprefix."counter (path, hitcount) VALUES('".trim($path)."',1)");
				return (int) ($rs->fields['hitcount']);
			}else{
				if($user_visited==0){
					$this->db->Execute("UPDATE ".$EASY_tableprefix."counter SET hitcount=hitcount+1 WHERE path='$path'");
					return (int) ($rs->fields['hitcount']+1);
				}else{
					return (int) ($rs->fields['hitcount']);				
				}
			}

		}elseif(is_int($val)&&(!empty($val))){
		
			if($rs->EOF){
				Echo "This counter ID does not exist. You must create one or choose a path.";
			}else{
				if($user_visited==0){
					$this->db->Execute("UPDATE ".$EASY_tableprefix."counter SET hitcount=hitcount+1 WHERE ID=$path");
					return (int) ($rs->fields['hitcount']+1);			
				}else{
					return (int) ($rs->fields['hitcount']);				
				}
			}
		}else{
		     echo "Enter Path or ID Parameter";
		}
		Return;
		
	}

	function getgifs($counternumber,$stylename){
		
		global $EASYWE_includepath;
		for($i=0; $i<strlen($counternumber);$i++){
			$out .= "<img src='".$EASYWE_includepath."images/".$stylename."/".substr($counternumber,$i,1).".gif' border=0>";
		}
		echo $out;
	}
}

?>
Return current item: Counter Class with graphics