如何删除codeigniter中的Duplicate where条件

I have list of inputs fields and two queries.

I want to run multiple queries for different type of records with same WHERE conditions

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

As you can see there is duplicate where conditions I want to remove that.

Use CI's 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

Use $this->db->flush_cache(); when you don't want to use cached 'where' any more.