CodeIgniter Forums
Custom where active record ??????? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Custom where active record ??????? (/showthread.php?tid=30707)



Custom where active record ??????? - El Forum - 05-24-2010

[eluser]Tom Taila[/eluser]
Ok so i have 3 fields in my table (user_name, friend_user_name, active) and i want to grab the rows of the database WHERE user_name OR friend_user_name == 'tom' AND active == 1

but i cant seem to figure out how to create a where clause with an OR clause, please help Sad thanks for your time Smile


Custom where active record ??????? - El Forum - 05-24-2010

[eluser]benurv[/eluser]
using CI Active Record it's kinda tricky, if i need this i write my query directly not using AR, i love AR but it's a pain for those kind of things

you could trying to concatenate your AND statement in the AR lines
$this->db->select('*',FALSE);
$this->db->from('table');
$this->db->or_where("user_name","tom");
$this->db->or_where("friend_user_name","'tom' AND active = 1");
$this->db->get('');

don't know if it will work, let us know...


Custom where active record ??????? - El Forum - 05-24-2010

[eluser]Tom Taila[/eluser]
Oh thanks, i didnt know about the 'or_where' method, i used that in my code like so:

Code:
function get_friends_list_for_own_profile()
    {
        $this->db->where('friend_user_name', $this->session->userdata('user_name'));
        $this->db->or_where('user_name', $this->session->userdata('user_name'));
        $this->db->where('active', 1);
        $query = $this->db->get('friends');
        return $query->result();
    }

and it worked, thanks so much


Custom where active record ??????? - El Forum - 05-24-2010

[eluser]WanWizard[/eluser]
Note that this query doesn't work without brackets:
Quote:( user_name = "tom" OR friend_user_name = "tom" ) AND active = 1
Your query will also return records with active != 1.


Custom where active record ??????? - El Forum - 05-24-2010

[eluser]Tom Taila[/eluser]
got it, sweet, thanks for the heads up Tongue