Welcome Guest, Not a member yet? Register   Sign In
Custom where active record ???????
#1

[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
#2

[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...
#3

[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
#4

[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.
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB