Welcome Guest, Not a member yet? Register   Sign In
Searching multiple tables - fields
#5

(This post was last modified: 10-20-2019, 11:12 AM by demyr.)

(10-16-2019, 11:00 PM)Wouter60 Wrote: Try this:

controller:
PHP Code:
$this->load->helper('array');
$this->load->model('company_model');
$data['city'] = 'New York';  //example of city in your search box
$data['category'] = 15;    //example of category_id in your search box
$s_result $this->company_model->search_result($data);
echo 
'<pre>';
print_r($s_result);
echo 
'</pre>'

company_model:
PHP Code:
public function search_result($data)
{
  $s_category element('category',$data,NULL);  //element is array helper function!
  $s_city element('city',$data,NULL);
  $s_name element('name',$data,NULL);

  $this->db
  
->select('cot.*,cit.*,cat.*')
  ->from('company_table cot')
  ->join('company_information_table cit','cot.company_id = cit.company_id')
  ->join('category_table cat','cot.category_id=cat.category_id');
  if ($s_category$this->db->where('cot.category_id',$s_category);
  if ($s_city$this->db->where('cit.company_city',$s_city);
  if ($s_name$this->db->where('cot.company_name',$s_name);
  $this->db->order_by('cot.company_id','ASC');
  $query $this->db->get();
  if ($query->num_rows() == 0) {
    return FALSE;
  }
  else {
    return $query->result();
  }


Thanks for your suggestions Wouter60. But I must say that I couldn't get the results I expected. 

After your suggestion I tried searching on all options (selects and input) one by one in the controller using IF statements and I got results for all of them except for one of them. However, it didn't seem healthy to me. That's why I had asked the question here. Then my friend (owner of the project) changed the search criteria and now it's easy for me because now every select dropdowns must be selected and input must be filled. However, it would be good to have a tutorial anywhere on web about searching regarding multiple criterias like we see on shopping or classified websites.
Reply


Messages In This Thread
Searching multiple tables - fields - by demyr - 10-16-2019, 02:41 AM
RE: Searching multiple tables - fields - by demyr - 10-16-2019, 01:24 PM
RE: Searching multiple tables - fields - by demyr - 10-20-2019, 11:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB