jeudi 3 mai 2018

CI3 Advanced Search Filter with Query Strings

I'm trying to implement an advanced search into my Codeigniter 3 site.

The current search functionality works fine (search all records).

I have a simple HTML form with one input (searchTerm), this sends a query string q=. My basic search syntax is;

$searchTerm  = $this->input->get('q'); 
if ($searchTerm) { 
    $this->db->from('records');
    $this->db->where('column1', $searchTerm); 
    $this->db->where('column2', $searchTerm);
    $this->db->where('column3', $searchTerm);
    // $this->db->where('etc', $searchTerm);
    $query = $this->db->get();
    return $query->result_array();
}

There are approximately 20 columns in my database.

I want to add an additional three inputs to my HTML form that will search these database columns;

  • collectionId
  • startYear
  • endYear

Do I need to create a new function for each possible search combination?

That will be a lot of if/else statements? For example;

 - if searchTerm is not empty
 - if searchTerm is not empty and startYear is not empty
 - if startYear is empty and endYear is not empty
 - etc
 - etc

Perhaps there is a more efficient way of doing this?

I don't want to use any additional libraries or plugins.

Any advice is appreciated.

Aucun commentaire:

Enregistrer un commentaire