CI分页双查询

时间:2013-11-01 06:59:20

标签: codeigniter

我在CI Framework工作。对于分页,我使用两个查询,一个用于使用限制的所需输出,另一个用于计算总行数。还有其他方法可以从单个查询中完成吗? 我的疑问是:

{
$searchtxt = $this->input->post('searchtxt');
        $datefrom = $this->input->post('fromdate');
        $todate = $this->input->post('todate');
        $status= $this->input->post('order_status');
        $sc_id = $this->input->post('sc_id');



    if($sc_id){
        $this->db->where('('.'o.requested_sc_id = '.$sc_id.' OR o.requesting_sc_id = '.$sc_id.')');


        //$this->db->where('o.requesting_sc_id =',$sc_id);
        }

    if ($status){
        $this->db->where('o.order_status =',$status);
        }
        if ($status != '' && $status == 0 ){
        $this->db->where('o.order_status = 0');
        }
    if($searchtxt){
        $this->db->like('o.order_number',$searchtxt);
    }
    if($datefrom)
    {           
        $this->db->where('o.order_dt >=', date("Y-m-d",date_to_timestamp($datefrom)));  
    }

    if($todate)
    {           
        $this->db->where('o.order_dt <=', date("Y-m-d",date_to_timestamp($todate)));    
    }
    if ($this->session->userdata('usergroup_id')!=1 && $this->session->userdata('usergroup_id')!= 2  && $this->session->userdata('usergroup_id')!=6 ){      

        $this->db->where('('.'o.requested_sc_id = '.$this->session->userdata('sc_id').' OR o.requesting_sc_id = '.$this->session->userdata('sc_id').')');
        //$this->db->where('o.requesting_sc_id = '.$this->session->userdata('sc_id'));
    }

    $this->db->select('o.order_id,o.order_number,o.order_dt,o.call_id,c.call_uid,o.order_status,o.order_dt,sc.sc_name,e.engineer_name');
    $this->db->from($this->table_name.' AS o');
    $this->db->join($this->mdl_callcenter->table_name.' AS c','c.call_id=o.call_id','left');
    $this->db->join($this->mdl_servicecenters->table_name.' AS sc' ,'sc.sc_id=o.requested_sc_id','left');
    $this->db->join($this->mdl_engineers->table_name.' AS e', 'e.engineer_id = o.engineer_id','left');
    $this->db->order_by('o.order_status ASC,o.order_dt DESC,o.order_created_ts DESC');
    if(isset($page['limit'])){
        $this->db->limit((int)$page['limit'],(int)$page['start']);
    }

    $result = $this->db->get();
        //echo $this->db->last_query(); 
    if ($status){
        $this->db->where('o.order_status =',$status);
        }
    if ($status != '' && $status == 0 ){
        $this->db->where('o.order_status = 0');
        }
    if($sc_id){
        $this->db->where('o.requesting_sc_id =',$sc_id);

        }
    if($searchtxt){
        $this->db->like('o.order_number',$searchtxt);
    }
    if($datefrom)
    {           
        $this->db->where('o.order_dt >=', date("Y-m-d",date_to_timestamp($datefrom)));  
    }

    if($todate)
    {           
        $this->db->where('o.order_dt <=', date("Y-m-d",date_to_timestamp($todate)));    
    }
    //if ($this->session->userdata('global_admin')!=1){     
    if ($this->session->userdata('usergroup_id')!=1 && $this->session->userdata('usergroup_id')!= 2  && $this->session->userdata('usergroup_id')!=6 ){  
        $this->db->where('o.requested_sc_id = '.$this->session->userdata('sc_id').' or o.requesting_sc_id = '.$this->session->userdata('sc_id'));
    }
    $this->db->select('o.order_id');
    $this->db->from($this->table_name.' AS o');
    $this->db->join($this->mdl_callcenter->table_name.' AS c','c.call_id=o.call_id','left');
    $this->db->join($this->mdl_servicecenters->table_name.' AS sc' ,'sc.sc_id=o.requested_sc_id','left');
    $this->db->join($this->mdl_engineers->table_name.' AS e', 'e.engineer_id = o.engineer_id','left');
    $result_total = $this->db->get();

    $orders['list'] = $result->result();
    $orders['total'] = $result_total->num_rows();
    return $orders;
}

1 个答案:

答案 0 :(得分:0)

以下是对此的一些解释。

function($limit,$offset){

    $this->db
            ->select("SQL_CALC_FOUND_ROWS emp", FALSE)
            ->select("*")
            ->from('tbl1')
            ->limit($limit, $offset);

    $data["query_result"] = $this->db->get()->result_array();

    $query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
    $data["total_rows"] = $query->row()->Count;

    return $data;
}

在这里,您会看到您被迫使用其他查询获得总计数 当然这不是你想要的,但使用mysql进行单一查询是不可能的 但这是一种php方式。

function($limit,$offset){

    $this->db
            ->select("*")
            ->from('tbl1');

    $data["query_result"] = $this->db->get()->result_array();
    $data["total_rows"] = array_slice($data["query_result"], $offset, $limit);

    return $data;
}

这可以通过单个查询来实现,但需要php才能获得所需的结果。