Login   Register  
PHP Classes
elePHPant
Icontem

File: counter_class.php3

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Pierre-henri Delaval  >  Counter  >  counter_class.php3  >  Download  
File: counter_class.php3
Role: ???
Content type: text/plain
Description: Class main page
Class: Counter
Author: By
Last change:
Date: 2000-06-14 08:16
Size: 4,957 bytes
 

Contents

Class file image Download
<?
/******************************************************************************/
//			Counter class
//	Author: Pierre-henri Delaval
//	Mail: dev-center@guepe.com
//	Date : 05/05/2000
//	Version: 1.0
//	Description:
//
//		This class simply creates a counter in the pages you wish
//		simply call the constructor with: $count = new Counter('<pagename>')
//		and the counter will be created for the current month.
//		It will be incremented each time you call the cronstructor.
//		Beginning a new month, a new line will be added in teh database
//		and now increment the counter for the next month.
//
//	Required:
//		- MetaBase class by Manuel Lemos <mlemos@acm.org> at http://phpclasses.upperdesign.com/browse.html/package/20
//		- a database supported by MetaBase 
//
//	Database structure:
//	Table: counter :
//		- id (integer auto_increment or serial or...)
//		- counts (integer)
//		- month (char 7)
//		- id_counter (char 20)	
//
//	I don't provide the sql script  due to the fact that you probably use Mysql
/*****************************************************************************/

class Counter {

// attributes

    var $numrow;
    var $result;
    var $error;
    var $database;
    var $row;

    var $hostname;
    var $username;
    var $password;
    var $dbname;
    var $counter_id;
    var $month;

// Constructor: 
// change the default host, user, pass, db  to fetch you database settings
// or pass theses parameter each time you call the constructor
// Change the Type: to fetch your database ( ifx for informix here )

    function Counter ($counter_id=0,$host="hostname",$user="webuser",$pass="webpass",$db="dbname",$TextAsVarchar=0) {

        if (strcmp($counter_id,"0") != 0) {
        $this->counter_id = $counter_id;
        $this->hostname = $host; $this->username= $user;
        $this->password = $pass; $this->dbname = $db;
        $this->row=0;

        $this->month = date("Ym");

        $this->error = MetabaseSetupDatabase(array(
        "Type"=>"ifx",
        "User"=>$this->username,
        "Password"=>$this->password,
        "Persistent"=>"0",
        "Host"=>$this->hostname,
        "Options"=>array(
        "TextAsVarchar"=>$TextAsVarchar
        )
        ),&$this->database);

        if($this->error!="")
        {
            echo "Error while setting this->database access instance: $this->error";
            print MetabaseError($this->database);
            return (1);
        }
        else {
            MetabaseSetDatabase ($this->database,$this->dbname);

            if ($this->IsNewMonth())
                 $this->CreateNewMonth();
            $this->Increment();

            return (0);
        }
        }
        else {
              echo "Error, no counter id passed";
              return (1);
        }
    }


// Privates methods

    function Increment() {
       $query = "update counter set counts = counts + 1 where id_counter = '";
       $query = $query . $this->counter_id. "' and month = '" . $this->month . "'";
       $this->Query($query);
    }

    function IsNewMonth() {
       $query = "select count(*) as month_num  from counter where  id_counter = '";
       $query = $query . $this->counter_id. "' and month = '" . $this->month . "'";
       $this->Query($query);

       if (MetabaseFetchResult($this->database,$this->result,$this->row,'month_num') > 0)
           return (0);
       else
           return (1);
    }

    function CreateNewMonth() {
       $query = "insert into counter (id,counts,month,id_counter) values (0,0,'" . $this->month . "','" . $this->counter_id ."')";
       $this->Insert($query);
    }

// Db access methods

    function Query ($query_string) {
       $this->result =  MetabaseQuery($this->database,$query_string);
       if (!$this->result) {
           print "<BR>Query: $query_string failed<BR>";
           print MetabaseError($this->database);
           return (1);
       }
       else {
           $this->numrow = MetabaseNumberOfRows($this->database,$this->result);
           return (0);
       }
    }

    function Insert ($query_string,$params = 0) {
        $prepared_query =  MetabasePrepareQuery($this->database,$query_string);

        if (is_array($params)) {
                for ($i=0;$i<count($params);$i++) {
                              MetabaseQuerySet($this->database,$prepared_query,$i+1,"text",$params[$i]);
                }
        }

        $this->result =  MetabaseExecuteQuery($this->database,$prepared_query);

        if (!$this->result) {
            print "<BR>Query: $query_string failed<BR>";
            print MetabaseError($this->database);
            return (1);
        }
        else {
            $this->numrow = MetabaseNumberOfRows($this->database,$this->result);
            return (0);
        }
    }




}
?>