Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] HowTo: Array/multiple words in POST that goes to a searchfunction
#1

[eluser]Devon Lambert[/eluser]
Hello CI community,

I am hoping that someone can point me in the right direction here. I have my paginated search class working properly when searching for 1 word. However, when I attempt to search for more than 1 word, it all goes to hell and it throws back an invalid/empty search.

--------------
I was actually able to solve this by doing the following in my Search Model:

MODEL:

Code:
function getSearchResults ($keyword, $limit, $offset)

    {


        if(isset($keyword)){

            if (explode(" ", $keyword) == FALSE) {
                        //Regular Search Stuff goes here...
                        }
                        
                        else {            

                $kt=split(" ",$keyword);//Breaking the string to array of words

                                

                // Now let us generate the sql

                while(list($key,$val)=each($kt)){

                    

                    if($val<>" " AND strlen($val) > 0) {

                        

                        $this->db->or_like('name', $val);

                    }

                }// end of while

                

                //$query = $this->db->query($q);

                $this->db->orderby('name', 'ASC');

                $result = $this->db->get('MY_TABLE',  $limit, $offset);

                

                $output = $result->result_array();

                return $output;

            }

        }

            
else { return 0; }
}
//...Finish off the search and count up the rows if you plan on paginating the results.

Turns out you were right on the money CJunky!

Hope it might help someone else out. :-)
#2

[eluser]thinkigniter[/eluser]
Interesting...
I've never had to do a multi word search script so where both learning here.
So it works for one work but not many.

Lets say where looking for duke nukem

Well $keyword isn't {explode}d so it's only looking for a single word duke nukem with the space?

or one work "dukenukem".

What if we explode the $keyword and look in the format of LIKE 'duke' AND LIKE 'nukem'

therefor looking for both words in a result.

Then look for individual results or_like('duke'), or_like('nukem')

What do you think?
#3

[eluser]Devon Lambert[/eluser]
Thanks for the response CJ.

That is one scenario, but not the perfect solution either. Take for instance, the search used right here in CI. I can enter multiple terms and it works just fine, spitting back all of the results that contain/match my posted input.

I'll have to dig into this one further tomorrow morning, but I think you may on the right path. :-)




Theme © iAndrew 2016 - Forum software by © MyBB