Welcome Guest, Not a member yet? Register   Sign In
Searching with AND (OR)
#1

[eluser]Henry Weismann[/eluser]
How would I do this with active record?:

Code:
SELECT * FROM (`ww_syslog`) WHERE ( `type` = 'user'  AND  `user` = '1' )  AND  (`ip`  LIKE '%lk%'
            OR  `action`  LIKE '%lk%'
            OR  `description`  LIKE '%lk%'
            OR  `date`  LIKE '%lk%') ORDER BY `date` desc LIMIT 5


Notice that we need to have the parentheses around all the OR's after the AND in order to limit the type and user but include the search phrase for ip, action, description and date.

Is this possible with the active record it seems it was not adding the parentheses and was therefore including all users.
#2

[eluser]Thorpe Obazee[/eluser]
Code:
$this-db->where("( `type` = 'user'  AND  `user` = '1' )  AND  (`ip`  LIKE '%lk%'
            OR  `action`  LIKE '%lk%'
            OR  `description`  LIKE '%lk%'
            OR  `date`  LIKE '%lk%')");
$this->db->order_by('date', 'desc');
$this->db->get(`ww_syslog`, 5);
#3

[eluser]Henry Weismann[/eluser]
Thanks I guess that is better then using

Code:
$this->db->query("SELECT *
FROM (`ww_syslog`)
WHERE ( `type` = 'system' )
AND  (`ip`  LIKE '%henry%'
OR  `action`  LIKE '%henry%'
OR  `description`  LIKE '%henry%'
OR  `date`  LIKE '%henry%')
ORDER BY `date` desc
LIMIT 5" );

So does that mean there is no way to represent that in active record without writing a custom query in the where function.
#4

[eluser]Henry Weismann[/eluser]
This is what I settled on in the end:


Code:
$this->db->order_by($sort, $direction);
$this->db->where('type', $type);
$this->db->where('user', $user);
$this->db->where("
(`ip`  LIKE '%$query%'
OR  `action`  LIKE '%$query%'
OR  `description`  LIKE '%$query%'
OR  `date`  LIKE '%$query%')",NULL,FALSE);
        
$this->db->get('syslog', $limit, $offset);

If anyone has a better way feel free to share.




Theme © iAndrew 2016 - Forum software by © MyBB