如何删除codeigniter中的Duplicate where条件

时间:2015-09-01 07:41:57

标签: php codeigniter

我有输入字段列表和两个查询。

我想针对具有相同WHERE条件的不同类型的记录运行多个查询

<?php
$tbname='xyz';
$field1=$this->input->post('field1');
$field2=$this->input->post('field2'); //I have list of inputs like this
$field3=$this->input->post('field3');
$order=$this->input->post('order');
$sort=$this->input->post('sort');
$pagination=true;
$fields = "x,y,z"; 
$this->db->select($fields); // select list of table column
// Creating where condition
$this->db->order_by($order ,$sort);

(!empty($field1)) ?    $this->db->where('field1', $field1) : '';
(!empty($field2)) ?    $this->db->where('field2', $field2) : '';
(!empty($field3)) ?    $this->db->where('field3', $field3) : '';
$data['scount']=$this->db->count_all_results($tbname);


// Creating where condition again
$this->db->order_by($order ,$sort);

(!empty($field1)) ?    $this->db->where('field1', $field1) : '';
(!empty($field2)) ?    $this->db->where('field2', $field2) : '';
(!empty($field3)) ?    $this->db->where('field3', $field3) : '';
  if($pagination!="false"){

    $this->db->limit($limit,$start) ;
    $config['total_rows'] = $data['scount'];
    $config['per_page'] = $limit; 
    $config['num_links'] = 15;
    $config['is_ajax_paging']      =  TRUE; // default FALSE
    $config['paging_function'] = 'ajax_paging'; // Your jQuery paging
    $config['first_link'] = '&laquo;';
    $config['full_tag_open'] = '<div class="pagination">';
    $config['full_tag_close'] = '</div>';
    $this->pagination->initialize($config);
    $data['pagination']= $this->pagination->create_links();
    $query= $this->db->get($tbname); // get records from table
    $data['is_pagination']=$pagination;
    $data['fields']=$fields;
    $record=$query->result_array();
    $data['records']=$record;
    $this->load->view('applicationtable',$data);
}
?>

正如您所看到的那样,我想删除的条件有重复。

1 个答案:

答案 0 :(得分:0)

使用CI&#39; Active Record Caching.

$this->db->start_cache();
(!empty($field1)) ?    $this->db->where('field1', $field1) : '';
(!empty($field2)) ?    $this->db->where('field2', $field2) : '';
(!empty($field3)) ?    $this->db->where('field3', $field3) : '';
$this->db->stop_cache();
$data['scount']=$this->db->count_all_results($tbname);
...
...
$query= $this->db->get($tbname); // get records from table

当您不想使用缓存时,请使用$this->db->flush_cache();其中&#39;更多。