| | |
Search with multiple search terms used
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
Hi. I tried looking for this problem on Google, and found a lot, but nothing that actually seems to work. I basically have a search script that works fine when using one keyword, but when one uses more than that, it returns every single row per keyword. So a search for "hello kitty" doesn't look for "hello" and "kitty", but for "hello" and then another time for "kitty".
I know there's something wrong with my code, but, what? And how can I solve this issue.
Can someone help me?
I know there's something wrong with my code, but, what? And how can I solve this issue.
PHP Syntax (Toggle Plain Text)
// get the search variable from URL $var = @$_GET['q'] ; // trim whitespace from the stored variable $trimmed = trim($var); // separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // build SQL Query for each keyword entered foreach ($trimmed_array as $trimm) { $query = "SELECT * FROM (articles LEFT JOIN authors ON articles.author_id = authors.author_id) WHERE text LIKE '%$trimm%' ORDER BY issue DESC" ; // and then the queries and everything; with which nothing's wrong }
Can someone help me?
Last edited by empoor; Sep 17th, 2009 at 5:16 pm. Reason: clarification
Explode on spaces and use ORs for the extra terms. http://php.net/explode
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
I'm already using explode to separate terms. And shouldn't it be "AND", 'cause I want the results to match all the results. It's basically already doing an "OR" search, which results in duplicate search results showing.
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
I have solved the issue myself by trying to fix things with experimentation.
My first problem was that the code I used to remove duplicate entries from the search array was basically looping twice. So, I edited the source to keep it from looping twice.
And then I use newarr to execute the search query (with foreach).
Secondly, because this is my first experience with building a search page, I used stock code and didn't notice that it was erroneously building a query for each keyword entered, instead of a query that incorporates all keywords (if multiple). So I removed , and wrote a little code to separate each keyword with AND.
And now it finally works.
My first problem was that the code I used to remove duplicate entries from the search array was basically looping twice. So, I edited the source to keep it from looping twice.
PHP Syntax (Toggle Plain Text)
do { $adid_array[] = $row['page_id']; } while($row = mysql_fetch_array($numresults));
PHP Syntax (Toggle Plain Text)
$tmparr = array_unique($adid_array); $i = 0; foreach ($tmparr as $newarr_v) { $newarr[$i] = $newarr_v; $i++; }
And then I use newarr to execute the search query (with foreach).
Secondly, because this is my first experience with building a search page, I used stock code and didn't notice that it was erroneously building a query for each keyword entered, instead of a query that incorporates all keywords (if multiple). So I removed
PHP Syntax (Toggle Plain Text)
foreach ($search_array as $search_keyword)
PHP Syntax (Toggle Plain Text)
$var = addslashes(htmlspecialchars($_GET['q'])) ; // trim whitespace from the stored variable $trimmed = trim($var); // separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // count keywords $trimm_total = count($trimmed_array); $i = 0; $searchstring = ''; // looping to get the search string foreach ($trimmed_array as $trimm) { if ($i != 0 and $i != $wordcount) { $searchstring .= " AND "; } $searchstring .= "text LIKE '%$trimm%' OR page_title LIKE '%$trimm%' OR author LIKE '%$trimm%'"; // incrementing the value $i = $i + 1; }
And now it finally works.
Last edited by empoor; Sep 19th, 2009 at 5:29 am. Reason: clarification
•
•
Join Date: Dec 2008
Posts: 33
Reputation:
Solved Threads: 1
Try this
php Syntax (Toggle Plain Text)
// get the search variable from URL $var = @$_GET['q'] ; // trim whitespace from the stored variable $trimmed = trim($var); // separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // build SQL Query for each keyword entered $query = "SELECT * FROM (articles LEFT JOIN authors ON articles.author_id = authors.author_id) WHERE"; $first = true; foreach ($trimmed_array as $trimm) { if(!$first) { $query . = " AND "; } else { $first = false; } $query = $query . " text LIKE '%$trimm%' " ; // and then the queries and everything; with which nothing's wrong } $query = $query. " ORDER BY issue DESC";
Last edited by dasatti; Sep 19th, 2009 at 7:38 am.
maSteR of aLL, jAck oF NoNe
![]() |
Similar Threads
- Unwanted search bar at bottom of screen (Viruses, Spyware and other Nasties)
- php search script - multiple search terms? (PHP)
- News Story: Google Sounds off on New Audio Search Tool (Search Engine Optimization)
- News Story: Google and Cuil search giants go head to head in DaniWeb testing (Pay-Per-Click Advertising)
- Using ASP.NET to create multiple search engine (ASP.NET)
- multiple parameter search (PHP)
- multiple search terms+fulltext search (MySQL)
- VB6 - "FindFirst" with multiple search Criteria (Visual Basic 4 / 5 / 6)
- Is it possible to block certain search terms from Google? (Search Engine Optimization)
- News Story: Google Book Search no bad thing, says fed up author (Pay-Per-Click Advertising)
Other Threads in the PHP Forum
- Previous Thread: create subdomain in server dynamically using php
- Next Thread: Help debug Random property script.
| Thread Tools | Search this Thread |
adsense adult advertising ajax api apple array asp bing book business c# c++ calendar checkbox cms cuil curl data database delete development display dynamic earth email engine facebook flash form forms google hacking html ibm image include insert internet iphone java javascript jquery keywords lamp legal limit link linux login malware marketing mcafee menu microsoft migrate monetization msn multiple mysql mysqlquery news oop oracle php post privacy publishing query radio remote revenue script search searchengine security seo server session sex sms soap software spyware sql sun system table tutorial twitter universe upload video web website wiki wolframalpha yahoo yahoo! youtube







