CodeIgniter Forums
Where if - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Where if (/showthread.php?tid=72830)



Where if - Srtymz - 02-16-2019

Excuse my bad English!
How can i use if statement in where Codeigniter


RE: Where if - Wouter60 - 02-16-2019

Tell us a bit more about the context.
What do you want to do, and for what do you need where and if?


RE: Where if - PaulD - 02-16-2019

You can just surround your query builder calls with your if statements.

Take a query that gets users that are members and paid:
PHP Code:
$this->db->from('users');
$this->db->where('user_status''member');
$this->db->where('user_payment_status''paid');
$users $this->db->get()->result_array(); 

Now you only want to check for paid if some variable is set, say $show_paid_only is true.
PHP Code:
$this->db->from('users');
$this->db->where('user_status''member');
if (
$show_paid_only === TRUE$this->db->where('user_payment_status''paid');
$users $this->db->get()->result_array(); 


If you are chaining your calls you just break the chain to insert the if statement.