Welcome Guest, Not a member yet? Register   Sign In
LIKE (sql statement) Question
#1

[eluser]xcristi[/eluser]
Hello,

I need to use a LIKE sql statement in CI. I use Active Records, so I have

Code:
$this->db->like('ci_name', strtoupper($id));

and this generates code like '%$id%'... OK.

My question is: it there a posibility to generate code like '$id%' or '%$id' (without % on both sides) ?

If not, I think I must recode some functions in DB_active_rec.php like below:

Code:
function like($field, $match = '', $side)
    {
        return $this->_like($field, $match, 'AND ', $side);
    }

The same for orlike() function.


Code:
function _like($field, $match = '', $type = 'AND ', $side = 3)
    {
        if ( ! is_array($field))
        {
            $field = array($field => $match);
        }
    
        foreach ($field as $k => $v)
        {
            $prefix = (count($this->ar_like) == 0) ? '' : $type;
            
            $v = $this->escape_str($v);
                    
                        switch ($side) {
                            case 1 : $this->ar_like[] = $prefix." $k LIKE '%{$v}'";break;
                            case 2 : $this->ar_like[] = $prefix." $k LIKE '{$v}%'";break;
                            case 3 : $this->ar_like[] = $prefix." $k LIKE '%{$v}%'";break;
                        }                
        }
        return $this;
    }

where 1 means left; 2 means right and 3 means both sides.

What do you think about this?


Thanks.
#2

[eluser]ELRafael[/eluser]
http://ellislab.com/forums/viewthread/61030/
#3

[eluser]xcristi[/eluser]
Ups... I didn't see that (that because of its weird title Smile )

So, after seeing all the solutions, I think the most elegant way is to improve the like function.
Thank you.
#4

[eluser]ELRafael[/eluser]
[quote author="xcristi" date="1190913936"]Ups... I didn't see that (that because of its weird title Smile )

So, after seeing all the solutions, I think the most elegant way is to improve the like function.
Thank you.[/quote]

Oh yeah. I hope in the future versions of CI, have a improve like this. That solutions, here in Brazil, we call POG - Programação Orientada a Gambiarras, or translate - WOP (Workaround-oriented Programming)




Theme © iAndrew 2016 - Forum software by © MyBB