Welcome Guest, Not a member yet? Register   Sign In
Active Queries Like
#1

[eluser]aslate[/eluser]
Hey there, i'm currently building a site which will have an event listing by location and a basic search functionality.

I want to have the ability to search by first letter (So searching by E will bring any events beginning with an E) for an A-Z listing, however it appears that CI's active-query for LIKE will replace:

Code:
$this->db->like('title', 'e')
with
Code:
title LIKE "%e%"
instead of
Code:
title LIKE "e%"

Is there any way i can use active-query in this way
#2

[eluser]Rwin[/eluser]
maybe...
Code:
$search = '%e';
$this->db->like('title', $search)


edit: wrong syntax for code =D
#3

[eluser]aslate[/eluser]
[quote author="Rwin" date="1185509454"]maybe...
Code:
$search = '%e';
$this->db->like('title', $search)


edit: wrong syntax for code =D[/quote]

It's a nice idea, but wouldn't CI in all it's mighty and intelligent glory end up simply replacing that with:

Code:
title LIKE "%\%e%"

And just escape the % sign? Doesn't it escape everything and therefore mean that it will "assume" you want to search for a string with the character "%e" in?
#4

[eluser]Phil Sturgeon[/eluser]
Code:
$search = '%e';
$this->db->where("title LIKE '$search'")

That'll do it!
#5

[eluser]aslate[/eluser]
[quote author="thepyromaniac" date="1185515913"]
Code:
$search = '%e';
$this->db->where("title LIKE '$search'")

That'll do it![/quote]

Okay, funky, i'll try that in the morning, thanks!

I know CI has a really great forum and community about, and obviously i appreciate the help, but is there any way of shoving a few things like this into the user guide? It seems to leap from User Guide to Wiki to Forum quite quickly.

And how does one contribute to those areas, because the biggest thing i'd like to add is that views have a 3rd parameter but i really think should be mentioned in the view section and not the load section.

Don't mean to sound like a grumpy so-and-so, but there's a couple of things i found through the forum i thought should've been elsewhere.

Anyway, really appreciate the help, i'm sure it's correct Big Grin
#6

[eluser]Phil Sturgeon[/eluser]
I understand what you mean. This isnt a very user contributed framework even though it is extremely awesome. This matter of improving the LIKE record has come up many times, however seeing as its a framework intended for programmers the user guide contains a fair amount of assument knowledge.

I'd personally like to see something like this added to /database/DB_active_rec:

Code:
/**
     * Like
     *
     * Generates a &#xLI;KE% portion of the query. Separates
     * multiple calls with AND
     *
     * @access    public
     * @param    mixed
     * @param    mixed
     * @return    object
     */
    function like($field, $match = '', $format = 'both')
    {
        return $this->_like($field, $match, $format, 'AND ');
    }
    
    // --------------------------------------------------------------------

    /**
     * OR Like
     *
     * Generates a &#xLI;KE% portion of the query. Separates
     * multiple calls with OR
     *
     * @access    public
     * @param    mixed
     * @param    mixed
     * @return    object
     */
    function orlike($field, $match = '', $format = 'both')
    {
        return $this->_like($field, $match, $format, 'OR ');
    }
    
    // --------------------------------------------------------------------

    /**
     * Like
     *
     * Called by like() or orlike()
     *
     * @access    private
     * @param    mixed
     * @param    mixed
     * @param    string
     * @return    object
     */
    function _like($field, $match = '', $format = 'both', $type = 'AND ')
    {
        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($format):
                case 'leading': $this->ar_like[] = $prefix." $k LIKE '%{$v}'"; break;
                case 'trailing': $this->ar_like[] = $prefix." $k LIKE '{$v}%'"; break;
                case 'both':     $this->ar_like[] = $prefix." $k LIKE '%{$v}%'"; break;
            endswitch;                                
            
        }
        return $this;
    }

Want to take guesses at how long it took me to add support for that third parameter?

Give a penny to the most accurate guess.

A) 20 seconds
B) 5 minutes
C) 6 hours of hardcore development, testing and re-testing.

If it were option a, reckon I could get this added into the core?

I know derek is busy with development but how hard would it be to add nifty little changes like this into the framework that save eveyone time and effort. I'd be happy to add this into the user manual myself and make everyone happy!




Theme © iAndrew 2016 - Forum software by © MyBB