如何在代码编写器中删除重复位置条件


how to remove Duplicate where condition in 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);
}
?>

正如你所看到的,有重复的地方我想删除它

使用CI的活动记录缓存

$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

当你不想再使用缓存的'where'时使用$this->db->flush_cache();