![]() |
Omitting where clause for empty criterion - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Omitting where clause for empty criterion (/showthread.php?tid=43115) |
Omitting where clause for empty criterion - El Forum - 06-30-2011 [eluser]cyberjunkie[/eluser] I'm using a form on my website to filter database records. For example I'm filtering posts by category tags and author. Code: SELECT * FROM (`posts`) WHERE `category` = 'technolgy' AND `author` = 'lila' AND `tags` = 'ebook' Filling all filtering option is not required so you can search posts just by author regardless of category or tags. that creates this query Code: SELECT * FROM (`posts`) WHERE `category` = '' AND `author` = 'lila' AND `tags` = '' The empty AND clause produces no results. According to the CI documentation, empty inputs are set to FALSE. Based on that, can omit empty clauses or add them only for filled inputs? In my model I'm using my input array for the where() function. Code: function filter($form_values) View Code: $form_values = array ( Omitting where clause for empty criterion - El Forum - 06-30-2011 [eluser]ontguy[/eluser] Are you asking for a way this could be done? Something like this should work. Code: $form_values = array(); Omitting where clause for empty criterion - El Forum - 07-01-2011 [eluser]cyberjunkie[/eluser] Thanks ontguy! That put me on the right track ![]() Code: $query = array_filter($your_form_input_array) and Code: $this->db->where($query) It will omit AND clauses if criteria is empty. |