顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2011年6月20日 星期一

crecord

class crecord extends crecord_NullObject {

    var $model;
    var $table;
    var $row;

    var $id;

    function crecord()
    {
        $this->model = NULL;
        $this->table = "";

        $this->_common_init();
    }

    function _common_init( $id = NULL )
    {
        $this->row = NULL;
        $this->id = $id;

        if( $id != NULL )
            $this->load($id);
    }

    function init( &$model , $table, $id = NULL)
    {
        $this->model = &$model;
        $this->table = $table;

        $this->_common_table();
        $this->load($id);
    }

    function equal($record)
    {
        foreach($record->row as $key => $value)
        {

            if( $this->getField($key) != $value )
                    return false;
        }
        return true;
    }


    function reuse($id = NULL)
    {
        $this->_common_init($id);
    }

    function loadlast( $key = null , $value = null)
    {
        $id = $this->model->select_max('id', $key, $value);
        $this->load($id);
    }


    function _common_table()
    {
        $this->model->init($this->table);
    }

    function load($id = NULL)
    {
         return $this->loadwithfield("id", $id);
    }

    function loadwithfield($field, $value)
    {
        $this->_common_table();
        $this->reuse();

        if( $field == NULL || $value == NULL )
            return;

        $row = $this->model->select_field_row($field,$value);

        return $this->_init_load_field($row);

    }

    function _init_load_field($row)
    {
        if( $row == NULL )
        {
            $this->id = NULL;
            return;
        }

        $this->id = $row->id;
        $this->setrow($row);

        return $this->row;
    }

    function load2($field ='id',$value=NULL)
    {
        return $this->loadwithfield($field, $value);

    }

    function is_update()
    {
        return $this->id != NULL;
    }

    function save()
    {
        if( $this->row == NULL )
            return;

        $this->_common_table();
        if( $this->id == NULL )
        {
           $this->id = $this->model->insert($this->row);
           return $this->id;
        }
        else
        {
           $this->model->update($this->id, $this->row);
           return $this->id;
        }

    }

    function delete($id = null)
    {
        $this->_common_table();


        if( $id != null )
            $this->load($id);

        if( $this->id == NULL )
            return;

        $this->model->delete($this->id);
        return $this->id;
    }

    function getField($field)
    {
        if( $field == "id" )
        {
            return $this->id;
        }

        switch( gettype($this->row) )
        {
            case "array":
                return $this->row[$field];

            case "object":
            {
                if( isset($this->row->$field) )
                    return $this->row->$field;
            }

            default:
                return;
        }

        if( is_object($this->row))
                return $this->row->$field;

        if( is_array($this->row))
                return $this->row[$field];
    }


    function setField($field, $value)
    {
        $type = gettype($this->row);
        switch( $type)
        {
            case "array":
                return $this->row[$field] = $value;
            case "object":
            default:
                return $this->row->$field = $value;
        }
    }

    function getrow()
    {
        $row = $this->row;
        if( $row != NULL )
            $row->id = $this->id;
        return $row;
    }

    function setrow($row)
    {
        $this->_tranArr2Obj ($row);
    }

    function _tranArr2Obj($array_row)
    {
        foreach($array_row as $key => $value )
        {
            if( $key == "id" )
                continue;
            $this->setField($key, $value);
        }
    }

    function create_owner()
    {
        return new crecord;
    }

    function isNull()
    {
        return "FALSE";
    }
    
}

mymodel_db

class mymodel_db extends model{

    var $table;

    function mymodel_db(){
        parent::model();

        $this->table = "";
    }

    function init($table)
    {
        $this->table = $table;
    }

    function select($id)
    {
        $row = $this->select_field_row("id", $id);
        return $row;
    }

    function select_query($field, $value)
    {
        $this->db->flush_cache();
        $this->db->where($field , $value);
        
        $query = $this->db->get($this->table);
        return $query;

    }


    function select_field_row($field, $value)
    {
        $query = $this->select_query($field, $value);
        return $query->row();

    }


    function select2($field,$value)
    {

        return $this->select_field_row($field, $value);
    }

    function delete($id){
        $this->db->flush_cache();
        $this->db->where("id",$id);
        $this->db->delete($this->table);

    }

    function insert($row)
    {
        $this->db->flush_cache();
        $this->db->insert($this->table,$row);
        $id = $this->db->insert_id();

        return $id;

    }

    function update($id, $row)
    {
        $this->db->flush_cache();
        $this->db->where("id",$id);
        $this->db->update($this->table,$row);
    }

    function selectall()
    {
        $this->db->flush_cache();
        $query = $this->db->get($this->table);
        return $query;
    }

    function select_all_idTOarr()
    {
        $id_arr = array();
        $this->db->flush_cache();
        $query = $this->db->get($this->table);
        foreach($query->result() as $row)
        {
            $id_arr[] = $row->id;
        }
        return $id_arr;
    }

    function like_query($field, $value)
    {
        $this->db->flush_cache();
        $this->db->like($field , $value);
        $query = $this->db->get($this->table);
        return $query;
    }

    function list_fields()
    {
        $result = $this->db->list_fields($this->table);
        return $result;
    }
}

2011年5月19日 星期四

cweb_grid

class cweb_grid {

    var $column_obj; // 欄位
    var $item_arr; // 項目列
    var $param; // 參數列

    function cweb_grid()
    {

        $this->column_obj = new empty_object;
        $this->item_arr = array();
        $this->param = array();
        
    }


    function _pre_html()
    {
        $param_str = "";
        foreach($this->param as $key => $value )
        {
            $param_str .= " {$key}=\"{$value}\" ";
        }

        $result = "<table " . $param_str . " >";

        return $result;

    }

    function _after_html()
    {
        return "</table>";
    }

    function _arr2obj($row)
    {
        return $row;
    }

    function _proc_row($row)
    {
        $result = "";
        foreach($this->column_obj as $key => $value)
        {
            if( !isset($row->$key) )
                $result .= "<td ></td>";
            else
                $result .= "<td >{$row->$key}</td>";
        }

        $result = "<tr> {$result} </tr>";

        return $result;

    }

    function _proc_column()
    {
        return $this->_proc_row($this->column_obj);

    }

    function _proc_item()
    {

        $result = "";
        foreach($this->item_arr as $item)
        {
            $result .= $this->_proc_row($item);
        }

        return $result;
    }

    function insert_column($key, $text)
    {
        $this->column_obj->$key = $text;
    }

    function insert_item($row)
    {

        $obj = $this->_arr2obj($row);
        $this->item_arr[] = $obj;

    }

    function add_param($key, $value)
    {
        $this->param[$key] = $value;
    }

    function outHtml()
    {
        $result = "";
        
        $result .= $this->_pre_html();

        $result .= $this->_proc_column();

        $result .= $this->_proc_item();

        $result .= $this->_after_html();

        return $result;
    }

}

2011年5月18日 星期三

cweb_link

class cweb_link
{
    var $id;
    var $url;
    var $text;
    var $display;
    function cweb_link($id = "")
    {
        $this->id = $id;
        $this->url = null;
        $this->text = null;
        $this->display = null;
    }

    function insert($url=null,$text=null,$display=true)
    {
        if($url == null || $text == null)
            return;
        $this->url = $url;
        $this->text = $text;
        $this->display = $display;
    }

    function outHtml()
    {
        if($this->display==false)
            return "";

        $html = "<a id='{$this->id}' href='{$this->url}'>{$this->text}</a>";
        if($this->id == "")
            $html = "<a href='{$this->url}'>{$this->text}</a>";
        
        return $html;
    }
}

2011年5月17日 星期二

cweb_select

class cweb_select {
    //put your code here

    var $name;
    var $option;
    var $selected;

    function cweb_select($name = "")
    {
        $this->option = array();
        $this->selected = null;
        $this->name = $name;
    }

    function insert($row /*array or object*/ )
    {
        foreach ($row as $key => $value )
        {
            $this->insertItem($key, $value);
        }
    }

    function insert_only_value($row /*array or object*/ )
    {
        foreach ($row as $value )
        {
            $this->insertItem($value, $value);
        }
    }

    function insertItem($value , $text)
    {
        $this->option[$value] = $text;
    }

    function selected($value)
    {
        $this->selected = $value;
    }

    function getText($value)
    {
        return $this->option[$value];
    }

    function outHtml()
    {
        $result = "";


        foreach( $this->option as $value => $text )
        {
            $selected = "";
            if( $this->selected == $value)
                $selected = " selected";

            $result .= "<option value='{$value}'{$selected}>{$text}</option>";
        }

        $result = "<select name='{$this->name}' >{$result}</select>";

        return $result;
    }
}


2010年2月16日 星期二

CuHtmlGeneric

//


class CuHtmlGeneric
{
    function OnHTML()
    {
        
    }   
}



class CuHTMLString extends CuHtmlGeneric
{
    var $m_string;
    
    function CuHTMLString()
    {
        $this->m_string = "";
    }
    
    function OnAdd($str)
    {
        $this->m_string .= $str;
    }
    
    function OnHTML()
    {
        return $this->m_string;
    }
}

class CuWebControl extends CuHtmlGeneric
{
    var $Attribute;
    
    function CuWebControl()
    {
        $this->Attribute = array();
    }
    
    
    function __set($member, $val){
        $this->Attribute[$member] = $val;
    }
    
    function Set($member, $val)
    {
        $this->__set($member, $val);
    }
    
    function __get($member)
    {
        return $this->Attribute[$member];
    }
    
    function Get($member)
    {
        return $this->__get($member);
    }
    
    function Remove($member)
    {
        $Attribute = $this->Attribute[$member];
        unset($this->Attribute[$member]);
        return $Attribute;
    }
    
    function MakeAttribute()
    {
        foreach($this->Attribute as $key => $value)
        {
            if( $value != "" )
                $str = "$key=\"$value\" ";
            else
                $str = "$key ";
            
            $html .= $str;
        }
        
        return $html;
    }
    
    
    function OnHTML()
    {
        $html = "<" . $this->MakeAttribute() . ">";
        return $html;
    }
    
}

/************************************************
* 類別名稱: CuHTMLAnchor
* 類別說明: 將HTML 的 連結控制項 Tag <a>
************************************************/
class CuHTMLAnchor extends CuWebControl
{
    
    function CuHTMLAnchor()
    {
        //CuHTMLAnchor
    }
    
    function OnHTML()
    {
        
        $value = $this->Remove("value");
        
        $html = "<" . $this->MakeAttribute() . ">" . $value . "</a>";
        
        return $html;
    }
}

支援 CuPage 的相關類別, 控制項類別, 組合字串類別, 方便用來操做HTML組裝

2010年2月15日 星期一

CuPage

// Task.form.html
<table><tr><td>
    <form method="POST" action="">
<input type="hidden" name="msg" value="#?msg?#">

    主旨:<input type="text" name="title" size="80"></p>
    <p>內容:<br>
    <textarea rows="5" name="description" cols="80"></textarea></p>
    <p><input type="submit" value="送出" name="B1"><input type="reset" value="重新設定" name="B2"></p>
    </form>
</td></tr></table>



//

class CuTestTaskForm extends CuPage 
{
    function OnProcess()
    {
        $this->title->Set("value", "哦哦更爽的");
        $this->msg->Set("value", "這樣行嗎?");
    }
    
}

$page = new CuTestTaskForm;

$filename = "resource/Task.form.html";
$page->LoadFile($filename);


$nIndex = $page->GetObjectIndex("msg");
$this->debug->debug($nIndex, "msg nIndex");
$this->assertTrue($nIndex == 5, "msg nIndex from CuPageGetObjectIndex");

$msg = $page->GetObject("msg");
$msg->Set("value", "抓出來的是考備版");
$this->debug->debug($msg, "CuPage::GetObject 抓出來的是考備版");
$page->SetObject($msg); // 再蓋回去囉
$this->debug->innerText($page->OnHTML(), "CuPage->SetObject 再蓋回去囉");

$page->OnProcess();
$this->debug->innerText($page->OnHTML(), "from CuPage::OnHTML");


// CuPage.php


/************************************************
* 類別名稱: CuPage
* 類別說明: 將HTML 的 form 載入後轉為PHP控制物件 CuWebControl
* 現在只支援 <INPUT >
************************************************/
class CuPage
{
    var $m_Control;
    var $m_Index;
    
    function CuPage()
    {
        $this->m_Control = array();
        $this->m_Index = 0;
    }
    

    /************************************************
    * 函式名稱: LoadFile
    * 函式說明: 載入html檔
    ************************************************/   
    function LoadFile($filename)
    {
        $html = LoadFile("resource/Task.form.html");
        $this->ExplodeForm($html);
        $this->CombineControl();
    }


    /************************************************
    * 函式名稱: ExplodeForm
    * 函式說明: 將Html Form 轉為 控制項陣列
    ************************************************/       
    function ExplodeForm($html)
    {
        $html_array = explode("<", $html);
        
        foreach( $html_array as $key => $value )
        {
            if( ereg( "^input", $value ) )
            {
                list($webcode, $str) = explode(">", $value);
                
                $object_Attribute = explode(" ", $webcode);
                $this->CreateWebControl($object_Attribute);
                
                $this->CreateHTMLString( $str );
            }
            else if( $value != "" )
            {
                $this->CreateHTMLString( "<" . $value );
            }
        }       
    }
    

    /************************************************
    * 函式名稱: CombineControl
    * 函式說明: 叫用這個函式, 來派發form 物件到類別內
    ************************************************/
    function CombineControl()
    {
        foreach( $this->m_Control as $key => $value )
        {
            if( $value->Attribute["name"] == "" )
                continue;
            
            $name = $value->Attribute["name"];
            $this->$name = &$this->m_Control[$key];
        }
        
    }
    
    
    /************************************************
    * 函式名稱: OnProcess
    * 函式說明: 子類別繼承這個函式後對表單的操作函式
    ************************************************/
    function OnProcess()
    {
            
    }
    
    
    /************************************************
    * 函式名稱: AddObject
    * 函式說明: 將CuWebControl加入CuPage陣列
    ************************************************/   
    function AddObject($object)
    {
        $this->m_Index = $this->m_Index + 1;
        $this->m_Control[$this->m_Index] = $object;       
    }
    
    
    /************************************************
    * 函式名稱: SetObject
    * 函式說明: 將CuWebControl加入CuPage陣列, 這裡會掃name參數, 找到一樣的就蓋掉, 找不到就轉叫 AddObject
    ************************************************/
    function SetObject($object)
    {
        
        $index = $this->GetObjectIndex($object->Get("name"));
        if( $index == 0 )
            $this->AddObject($object);
        else
            $this->m_Control[$index] = $object;

    }
    
    
    /************************************************
    * 函式名稱: GetObjectIndex
    * 函式說明: 依名稱尋找索引值
    ************************************************/
    function GetObjectIndex($name)
    {
        foreach( $this->m_Control as $key => $value )
        {
            if( $name == $value->Attribute["name"] )
                return $key;
        }
    }
    
    
    /************************************************
    * 函式名稱: GetObject
    * 函式說明: 依名稱尋找元件 -- 這裡傳回的是copy版, 
    ************************************************/   
    function GetObject($name)
    {
        
        foreach( $this->m_Control as $key => $value )
        {
            if( $name == $value->Attribute["name"] )
                return $this->m_Control[$key];
        }

    }   


    /************************************************
    * 函式名稱: CreateWebControl
    * 函式說明: 將處理好一半的HTML碼轉為CuWebControl, 並且叫用SetObject
    ************************************************/       
    function CreateWebControl($html_tag_array)
    {
        
        $Control = new CuWebControl;
        foreach($html_tag_array as $Attribute_key => $Attribute_value)
        {
            list($key, $value) = explode("=", $Attribute_value);
            $value = split("\"", $value);
            $Control->Set($key, $value[1]);
        }
        
        $this->AddObject($Control);
        
    }


    /************************************************
    * 函式名稱: CreateHTMLString
    * 函式說明: 建構HTML字串元件除了可以轉換的 FORM INPUT 外, 其他的文字直接建文字元件
    ************************************************/           
    function CreateHTMLString($string)
    {
        if( $string == "" )
            return;
        
        $Control = new CuHTMLString();
        $Control->OnAdd( $string );
        $this->AddObject($Control);
        
    }
    
    
    function OnHTML()
    {
        $html = "";
        foreach( $this->m_Control as $key => $value )
        {
            if( $value != null )
                $html .= $value->OnHTML();
        }
        return $html;
    }
    
}

Form 的載入一般都直接使用HTML並且替換字串, 在這裡將form.html當做外部介面設定檔,
將 input 生成為可控制類別, 使用類別操控的方式來處理表單, 可以很方便的操作內容元件,
上面拿 tag input 來做範例.

2010年2月14日 星期日

CuTaskDlg

//

//***************************************
// 類別名稱:CuTask
// 功  能:工作記錄 對資料表 task 資料維護
//***************************************/
class CuTaskDlg  {

    // 成員變數
    // private
    var $created;
    
    // public
    var $title;
    var $description;
    var $account_id;


    // 資料庫關聯
    var $ado;
    var $record;
    
    // 靜態變數
    var $table_name = "hrc_task";
    
    /************************************************
    * 函式名稱: CuTaskDlg
    * 函式說明: 使用CuRecord作業
    ************************************************/       
    function CuTaskDlg($ado) {

        $this->ado = $ado;
        $this->record = new CuRecord($ado, $this->table_name, "sn");

    }
    
    
    /************************************************
    * 函式名稱: Save
    * 函式說明: 儲存 這個 啞資料類別不處理
    ************************************************/   
    function Save()
    {
        if(!($this->title))
            return;
            
        
        $this->created = date("Y-m-d H:i:s", time());
        $record = &$this->record;
        
        // 檢查是否有留言過
        $sn = $record->Load("title", "$this->title");
        if( $sn != "" )
            return;
        
        $record->SetField("created", $this->created );
        $record->SetField("title", $this->title);
        $record->SetField("description", $this->description);
        
        
        $record->SetField("account_id", $this->account_id);
        $record->Save(); 
    }
    
    
    /************************************************
    * 函式名稱: Load
    * 函式說明: 指定SN值 載入指定的資料項
    ************************************************/       
    function Load($sn)
    {
        $record = &$this->record;
        $record->Load("sn", $sn);
        
        $this->created  = $record->GetField("created");
        $this->title = $record->GetField("title");
        $this->description = $record->GetField("description");
        $this->account_id = $record->GetField("account_id");
        
    }
    
}



/*

-- 
-- 資料表格式: `hrc_task`
-- 

CREATE TABLE `hrc_task` (
  `sn` int(11) NOT NULL auto_increment,
  `created` timestamp NOT NULL default '0000-00-00 00:00:00',
  `title` varchar(256) NOT NULL,
  `description` text,
  `account_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`sn`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 created 記錄建立日期
 title 項目主旨
 description 詳細內容
 account_id 帳號索引 用來關聯是那位使用者寫入的 預設為0 
*/


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 後. 在處理資料表時就簡明多了

2010年1月28日 星期四

CuMicroTime - the current timestamp with microseconds.

// the current timestamp with microseconds.



// CuMicroTime.php 


class CuMicroTime  {

    var $usec;
    var $sec;
    
    function CuMicroTime(/* CuMicroTime */ $MicroTime = null) {

        if( $this->CheckClassType($MicroTime) )
        {
            $this->usec = $MicroTime->usec;
            $this->sec = $MicroTime->sec;
        }
        else
        {
            list($this->usec, $this->sec) = explode(" ", microtime());
        }
    }
    
    function Dec( /* CuMicroTime */ $MicroTime)
    {
        
        if( CheckClassType($MicroTime) )
            return NULL;

        $usec = (float)$this->usec - (float)$MicroTime->usec;
        $sec = $this->sec - $MicroTime->sec;

        $usec = substr($usec, 1);
        return $sec.$usec;
        
    }
    
    function CheckClassType($MicroTime)
    {
        if( gettype($MicroTime) != 'object' || get_class($MicroTime) != "cumicrotime" )
            return false;
        else
            return true;
    }
    
    
}

2010年1月17日 星期日

CuTestUnit - Easy way to Unit Test.

// Easy way to Unit Test.



// CuTestUnit.php

class CuTestUnit
{
    var $debug;
    
    function __construct()
    {
        $this->debug = new CuDebugger; 
    }   
    
    function CuTestUnit()
    {
        $this->__construct();
    }
    
    function assertTrue($bool, $key = null )
    {
        if( !$bool || $bool != true)
            $result = "<font color = '#ff0000'>FALSE</font>";
        else
            $result = "<font color = '#0000ff'>OK</font>";
        
        $this->debug->debug($result,  $key);  
    }
    
    function assertEquals($value1, $value2, $key = null)
    {
        $this->assertTrue($value1 == $value2, $key);
    }
    
    function GetHTML()
    {
        return $this->debug->GetHTML();
    }
    
}

2010年1月14日 星期四

CuDebugger - Display PHP value about Array , Class detail information.

// Display PHP value about Array , Class detail information.

$debug = new CuDebugger
$debug->debug($result,  $key);


// CuDebugger.php

class CuDebugger  {

    var $m_Data;
    var $m_DataIndex;

    function CuDebugger() {
        $this->m_Data = array();
        $this->m_DataIndex = 0;
    }
    
    function debug($data, $key = null, $innerText = false)
    {
    
        $index = $this->m_DataIndex + 1;
        $this->m_Data[$index . " " .$key] = $data;
        $this->m_DataIndex = $index;
    }
    
    function innerText($data, $key = null)
    {
        $data = str_replace("<", "& lt;", $data);
        $data = str_replace(">", "& gt;", $data); 
        
        $this->debug($data, $key);
    }
    
    function GetHTML(){
        return $this->printArray($this->m_Data);
    }
    
    function printArray($var) {
        
        $title_string = "";
        $string_row = "";
        $string_value = "";
        $string = "";
        
        foreach($var as $key => $value){
            
            
            if( is_array($value) ) {
                $string_value .= $this->printArray($value, false);
            }
            elseif(gettype($value) == 'object') {
                $string_value .= "Object of class " . get_class($value) . " { ".serialize($value) . " } ";
            }
            elseif( $value ) {
                $string_value .= "$value";
            }
            else {
                $string_value .= "< BR >";
            }
            
            $string_row .= "<tr>\n<td><b>$key</b></td><td>$string_value</td></tr>\n";       
            $string_value = "";
            
        }
        
        if( $string_row ) {
            $string = '<table border = "1">'.$string_row;
            $string .= "</table>\n";
        }
        
        return $string;
        
        
    }   
}

2010年1月8日 星期五

CuRecord - Access Record

// Access Record

$record = new CuRecord($ado, $table_name, $key);

//
$record->SetField("field_name", $field_value );
$record->Save();

//
$record->Load("field_name", $field_value);
$value = $record->GetField("field_name");

//
$record->Load("field_name", $field_value);
$record->Destroy();


// CuRecord.php

class CuRecord  {

    var $ado;
    var $table_name; 
    
    var $key_name; 
    var $key_value; 
    
    var $database_fields;
    var $modified_fields;
    
    
    function CuRecord($ado, $table_name, $key_name = "sn" , $key_value = "") {
        
        $this->ado = &$ado;
        $this->table_name = $table_name;
        $this->key_name = $key_name;
        $this->key_value = $this->_check_input($key_value);
        
        $this->database_fields = array();
        $this->modified_fields = array();        
    }
    
    
    function SetField( $field, $value) {

        if( is_numeric($field) ) 
            return;
        
        $this->database_fields[$field] = $value;
        $this->modified_fields[$field] = true;

    }


    function GetField($field) {
        return $this->database_fields[$field]; 
    }   


    function Destroy() {
        
        if( !$this->key_value )
            return;
        return $this->ado->sql("DELETE FROM $this->table_name WHERE $this->key_name = '$this->key_value'");
        
    }   
    

    function Save()
    {
        
        if( $this->key_value )
        {
            $result = $this->_update();
        }
        else
        {
            $result = $this->_insert();
            $this->_load_key_value();
        }
        
        return $this->key_value;
    }
    

    function Load($bargain_field, $bargain_value)
    {
        
        if(!($bargain_field && $bargain_value ))
        {
            $bargain_field = $this->key_name;
            $bargain_value = $this->key_value;
        }
        
        if( !($bargain_field && $bargain_value ) )
            return;
        
        $value = $this->_check_input($bargain_value);
        $stmt = "`$bargain_field` = '$value'";
        $stmt = "SELECT * FROM $this->table_name WHERE $stmt";
         
        $result = $this->ado->sql($stmt);
        if( $result == "" )
            return ;
        
        $result_table = $this->ado->fetch_array();
        $this->database_fields = $result_table[0];
        
        if( sizeof($this->modified_fields) > 0 ) {
            foreach( $this->modified_fields as $key => $value ) {
                $this->modified_fields[$key] = false;
            }
        }       
        
        $this->key_value = $result_table[0][$this->key_name];
        
        return $this->key_value;
        
    }
    

    function _load_key_value()
    {
        $stmt = "SELECT MAX($this->key_name) AS $this->key_name FROM `$this->table_name` WHERE ";
        foreach( $this->database_fields as $key => $value ){
            if( !is_numeric($key) && $value ){
                if( $this->modified_fields[$key] == true ){
                    $value = $this->_check_input($value); 
                    $stmt .= "`$key` = '$value' AND ";
                }
            }
        } 
        
        $stmt = substr($stmt,0,strlen($stmt)-5);    
        $handle = $this->ado->sql($stmt);
        
        $result_table = $this->ado->fetch_array();
        $this->key_value = $result_table[0][$this->key_name];
        
        return $result_rows;
    }
    

    function _insert() {

        $stmt = "INSERT INTO `$this->table_name` ";
        
        foreach ($this->database_fields as $key => $value) {
            if( !is_numeric( $key )&& $value){
                $key = $this->_check_input($key);
                $key_stmt .= " `$key`,";
                
                $value = $this->_check_input($value);
                $value_stmt .= " '$value',";
                
            }
        }
        
        $key_stmt = substr($key_stmt,0,strlen($key_stmt)-1); 
        $value_stmt = substr($value_stmt,0,strlen($value_stmt)-1); 
        
        $stmt .= " (" . $key_stmt . ") VALUES (" . $value_stmt . ")" ;
        $handle = $this->ado->sql($stmt, "_insert");
        
        
        return $handle . " " . $stmt;
        
    }    
    
    
    function _update() {
        
        $field_value = $this->key_value;
        $field_name = $this->key_name;
        $table_name = $this->table_name;
        
        $stmt = "UPDATE `$table_name` SET ";
        foreach ($this->database_fields as $key => $value) {
            if( !is_numeric( $key ) ){
                if( $this->modified_fields[$key] == true ) {
                    $value = $this->_check_input($value);
                    if( $value == "" ){
                        $stmt .= "`$key` = 'NULL', ";
                    }
                    else {
                        $stmt .= "`$key` = '$value', ";
                    }
                }
            }
        }
        
        $stmt = substr($stmt, 0, strlen($stmt) - 2);
        $stmt .= " WHERE `$field_name` = '$field_value'"; 
        
        $handle = $this->ado->sql($stmt);
        return $handle . " " . $stmt;
        
    } 
    
    
    function _check_input($value)
    {
        if($value == "")
            return "";

        if (get_magic_quotes_gpc())
        {
            $value = stripslashes($value);
        }
        
        if (!is_numeric($value))
        {
            $value =  mysql_real_escape_string($value);
        }
        
        return $value;
    }
}

2010年1月6日 星期三

CuADO - connection to a MySQL Server

// connection to a MySQL Server

$this->ado = new CuADO;
$ado->Connect($host, $username, $password);
$ado->SelectDB($db); 


// CuADO.php


class CuADO  {
    
    var $link;
    var $query_handle;
    
    function CuADO() {
        
    }
    
    function Connect($host, $username, $password)
    {
        $this->link = mysql_connect($host, $username, $password);
        return $this->link;
    }
    
    function SelectDB($db)
    {
        return mysql_select_db($db, $this->link);
    }

    function sql($stmt, $debug_key = "")
    {
        $this->query_handle = mysql_query($stmt, $this->link) ;
        return $this->query_handle;
    }

    function fetch_array() {
        
        $query_handle = &$this->query_handle;
        $i = 0;
        while($result_rows[$i] = mysql_fetch_array( $query_handle ) ){
            $i++;
        }
        
        return $result_rows;
        
    }   
    
}