2010年2月13日 星期六

CuCount



// CuCount.php

class CuCount
{
    var $ado;
    var $count_name;
    var $record;
    
    function CuCount(/*CuAdo*/ $ado, $count_name)
    {

        $this->ado = &$ado;
        $this->count_name = $count_name;
        $this->record = new CuRecord(&$ado, "hrc_count", "id");
        $this->record->Load("name", $this->count_name);

    }
    
    function Inc()
    {
        
        $record = &$this->record;
        
        $count = $record->GetField("count");
        $count = $count + 1;
        $record->SetField("count", $count);
        $record->SetField("name", $this->count_name);
        $record->Save();
        
        $this->AddLog();
        $CountLog = $this->CountLog();
        

        $TotalCount = $record->GetField("totalcount");
        $TotalCount = $TotalCount + $CountLog;

        return $TotalCount;
        
    }
    
    function CountLog( $date = "" )
    {
        $id = $this->record->GetField('id');
        if( $date != "" )
            $created_stmt = " AND created = '$date' ";
            
            
        $stmt = "SELECT * FROM hrc_countlog WHERE `count_id` = '". $id ."'" . $created_stmt;
        $this->ado->sql($stmt);   
        $result_table = $this->ado->fetch_array();
        return count($result_table);
        
    }
    
    function AddLog()
    {
                
        $CountLog = new CuRecord(&$this->ado, "hrc_countlog", "id");
        $created = date("Y-m-d", time());       
        $CountLog->SetField("created", $created);
        $CountLog->SetField("count_id", $this->record->GetField("id"));
        $CountLog->SetField("user_agent",  $GLOBALS["HTTP_USER_AGENT"]);
        $CountLog->Save();
        
    }
    
}

/*
CREATE TABLE `hrc_count` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 64 ) NOT NULL ,
`count` INT( 11 ) NOT NULL ,
`totalcount` INT( 11 ) NOT NULL default '0',
INDEX ( `name` ) 
) ENGINE = MYISAM ;

CREATE TABLE `hrc_countlog` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`count_id` INT NOT NULL ,
`created` DATE NOT NULL default '0000-00-00',
`user_agent` varchar(256) NOT NULL,
INDEX ( `count_id` ) 
) ENGINE = MYISAM ;

*/


有了CuRecord & CuADO 後. 在處理資料表時就簡明多了

沒有留言:

張貼留言