Welcome Guest, Not a member yet? Register   Sign In
Active Records Brakets
#1

[eluser]SeameX[/eluser]
Hello Guys,
i want to have something like this:
Code:
SELECT * FROM x WHERE cid = 'x' AND (col = '%word%' OR col = '%word%')

how can i do the brakets?

So far i have this
Code:
$this->db->select('cid, name, node_cid, sort, active')->where('cid',$cid)

The user should be able to search for key words. There is a foreach doing this all the time:
Code:
$this->db->or_like($col, $word);

So i have everything but without brakets and thats not working. Is there a easy way to do it?

Best regards

SeameX
#2

[eluser]ηυмвєяσηє[/eluser]
Code:
$this->db->select('cid, name, node_cid, sort, active')->where('cid',$cid);

$sWhere = '';

foreach($cols as $col)
  $sWhere .= $col . " LIKE '%" . $word . "%' OR ";
$sWhere = substr_replace($sWhere, '', -3);

if($sWhere != '')
  $this->ci->db->where('(' . $sWhere .')');
#3

[eluser]SeameX[/eluser]
Thanks for the fast anwer. I have made myself a solution already.

Append this to system/database/DB_active_rec.php
Code:
function doBraketsLike()
    {
        // Change first
        $this->ar_like[0] = '('.$this->ar_like[0];

        // Change last
        $last = count($this->ar_like)-1;
        $this->ar_like[$last] .= ' )';
    }

This you need to call in the Model
Code:
$this->db->doBraketsLike();

He will do brakets arround all the Like entrys you have made before with the like() active record function. The solution is not the best because it edits Core files but it works fine.




Theme © iAndrew 2016 - Forum software by © MyBB