2011年6月20日 星期一

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;
    }
}

沒有留言:

張貼留言