We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,140 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Doing Multiple Keyword search with php

Hello

I have a database of jobs and I want persons to be able to search using keywords. users will be required to enter these keywords via text field. I am able to display the results if the user enters one search word in the text field however i am not sure how to perform the query if the user enters multiple keywords separated by a comma in the text area. please help

2
Contributors
1
Reply
44 Minutes
Discussion Span
6 Months Ago
Last Updated
2
Views
bitejones
Newbie Poster
1 post since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

There are probably better ways, but If I had to achieve it with existing knowledge, I would do something like this:

$wordString = $_POST['search'];
/* first test there are comma separated values */
if (strpos($wordString, ",")){
    /* split string into array of strings on the comma */
    $words = explode(",", $wordString);
    /* start the sql */
    $sql = "SELECT * FROM table WHERE";
    /* for each word update the query */
    foreach($words as $word){
        $sql.=" field LIKE '%$word%' OR";
    }
    /* remove trailing 'OR' from query */
    $sql = substr($sql, 0, -3);
} else {
    /* NO comma separated values, build normal query */
    $sql = "SELECT * FROM table WHERE filed LIKE '%$wordString%'";
}

If the user enters 'one,two,three', the resulting query would be:
"SELECT * FROM table WHERE field LIKE '%one%' OR field LIKE '%two%' OR field LIKE '%three%'", otherwise a normal query is constructed.

adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43
Solved Threads: 40
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0976 seconds using 2.7MB