Welcome Guest, Not a member yet? Register   Sign In
Find words form same column in Database (Search)
#1

(This post was last modified: 10-20-2015, 09:29 PM by ardavan.)

Hi,

im not sure my thread subject is clear or not. anyway I'm creating a search in my project.
Now I'm getting the words from search input into an array

User will key in ( php programming language ).

PHP Code:
$words explode(' '$search_text); 

Code:
$words = array('php', 'programming', 'language');

Now i need to find any TITLE match with 'php' or 'programming' or 'language'.

my DataBase is like this
Code:
TITLE         |   DESC   |   STATUS
====================================
php coding    |   blabh  |   LIVE
codeigniter 3 |   blaaaa |   LIVE
all language   |   blaaaa |   LIVE

I'm expecting to get the result like this
Code:
TITLE         |   DESC   |   STATUS
====================================
php coding    |   blabh  |   LIVE
all language   |   blaaaa |   LIVE
"Because we have php in the first title and we have language in the second title"

Im trying to get expected result like this
PHP Code:
$this->db->where_in('TITLE'$words); 

The Issue is i can only get one of my rows. (php coding)

How to make it?
Any idea?

Thanks a lot guys.
Reply
#2

(10-20-2015, 09:28 PM)ardavan Wrote: Hi,

im not sure my thread subject is clear or not. anyway I'm creating a search in my project.
Now I'm getting the words from search input into an array

User will key in ( php programming language ).


PHP Code:
$words explode(' '$search_text); 

Code:
$words = array('php', 'programming', 'language');

Now i need to find any TITLE match with 'php' or 'programming' or 'language'.

my DataBase is like this

Code:
TITLE         |   DESC   |   STATUS
====================================
php coding    |   blabh  |   LIVE
codeigniter 3 |   blaaaa |   LIVE
all language   |   blaaaa |   LIVE

I'm expecting to get the result like this

Code:
TITLE         |   DESC   |   STATUS
====================================
php coding    |   blabh  |   LIVE
all language   |   blaaaa |   LIVE
"Because we have php in the first title and we have language in the second title"

Im trying to get expected result like this

PHP Code:
$this->db->where_in('TITLE'$words); 

The Issue is i can only get one of my rows. (php coding)

How to make it?
Any idea?

Thanks a lot guys.

You could use like() and or_like()


PHP Code:
$this->db->like('TITLE'$words[0]);

for (
$i 1$i count($words); $i++)
{
    $this->db->or_like('TITLE'$words[$i]);

Reply
#3

(This post was last modified: 10-20-2015, 10:59 PM by ardavan.)

(10-20-2015, 09:53 PM)pdthinh Wrote: You could use like() and or_like()




PHP Code:
$this->db->like('TITLE'$words[0]);

for (
$i 1$i count($words); $i++)
{
    $this->db->or_like('TITLE'$words[$i]);


thanks for your help. i just needed to add
PHP Code:
$this->db->where('STATUS','Live'); 
before
PHP Code:
$this->db->or_like('TITLE'$words[$i]); 
Reply
#4

(10-20-2015, 10:38 PM)ardavan Wrote: thanks for your help. i just needed to add


PHP Code:
$this->db->where('STATUS','Live'); 
before


PHP Code:
$this->db->or_like('TITLE'$words[$i]); 

You could also do this:

PHP Code:
        $this->db->from('table')
            ->group_start()
                ->like('title''php')
                ->or_like('title''codeigniter')
            ->group_end()
        ->where('status''live');
        echo $this->db->get_compiled_select(); 
Reply
#5

For detailed information, please visit this https://www.codeigniter.com/user_guide/d...ilder.html
Reply
#6

(This post was last modified: 10-22-2015, 10:54 PM by ardavan.)

but I'm wonder why this is not working!
PHP Code:
$this->db->where_in('TITLE'$words); 
Reply
#7

(This post was last modified: 10-23-2015, 12:31 PM by ignitedcms.)

Give more of an example.

Sample input, expected output, actual output, and of course your code.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

where_in doesn't work because it is looking for an exact match to the array values. It means select titles that are in this array, not titles that have some bits found in this array.

So I am actually surprised you got any results at all. Unless the rest of your query gave you a result, you should have had a null result from that, since none of your titles are in that array you used as an example.

pdthinh gave the right way to do it.

The way to do what you are trying to do is with the like and or_like statements, with a loop through your search terms. So use the loop from pdthinh's solution and the groups from his later solution and that will do it.

Best wishes,

Paul.
Reply
#9

If you're not married to the active record approach, I suggest you craft the SQL using RLIKE which can take wildcards and be chained together. A simple loop could build out the clause you need from your array items.
Reply
#10

@Kirkja
I never knew about RLIKE, but wow, what a fantastic operator. I did a google on it and it has some fabulous properties.

Note to self: Learn more about MySql.

Best wishes,

Paul.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB