homepage Welcome to WebmasterWorld Guest from 54.205.242.179
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
explode function
can't explode keywords for search
brancook




msg:3538177
 10:27 pm on Dec 31, 2007 (gmt 0)

I'm trying to somehow fit the explode function into the code below but every time I put it in it doesn't work. When a phrase is entered with 2 or more keywords unless there is an exact match there are no results found. Here is my code so far:

<?php
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);

if (!$searchterm) {
echo 'You have not entered a search term. Please try again.';
exit;
}
if (get_magic_quotes_gpc()) {
$searchterm = stripslashes($searchterm);
}
$searchterm = mysql_real_escape_string($searchterm);

$query = "SELECT * FROM site_data WHERE description LIKE '%$searchterm%' OR page_title LIKE '%$searchterm%' OR keywords LIKE '%$searchterm%'";
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo "<p>Number of Results found for <i><strong>$searchterm</strong></i>: $num_results</p>";

for ($i=0; $i <$num_results; $i++) {
$row = mysql_fetch_assoc($result);
echo '<p>'.($i+1).': ';
echo '<strong>' . stripslashes($row['page_title']) . '</strong>';
echo '<br />';
echo substr($row['description'], 0, 255) . '...';
echo '<br />';
echo stripslashes($row['url']);
echo '</p>';
}
?>

I tried to enter this line in:

keywords = explode(" ", $searchterm);

but it doesn't seem to work.

 

jatar_k




msg:3538182
 10:56 pm on Dec 31, 2007 (gmt 0)

what happens? do you get an error?

I also assume this was a cut and paste error

$keywords = explode(" ", $searchterm);

you could also count spaces in the string, if there is a space then explode it, if not just carry on

brancook




msg:3538190
 11:11 pm on Dec 31, 2007 (gmt 0)

I don't get an error. Actually I typed that string out the wrong way when posting, I had "$keywords" in my code correctly. What actually happens is I keep getting the same 2 results from the database no matter what I type into the search box.

jatar_k




msg:3538191
 11:12 pm on Dec 31, 2007 (gmt 0)

have you tried echo'ing the query to see if it is getting constructed properly?

brancook




msg:3538208
 11:33 pm on Dec 31, 2007 (gmt 0)

When I insert this string: $keywords = explode(" ", $searchterm);

and do a search with a phrase where I know that if the words were searched independently I would get results here is my echoed query:

SELECT * FROM site_data WHERE description LIKE '%Array%' OR
page_title LIKE '%Array%' OR keywords LIKE '%Array%'

not sure where Array is coming from?

jatar_k




msg:3538239
 1:53 am on Jan 1, 2008 (gmt 0)

because $keywords is an array and you need to take each word out individually

what are you wanting to do with the multiple words? make multiple searches?

or do you want to use more ORs in your query?

brancook




msg:3538260
 3:55 am on Jan 1, 2008 (gmt 0)

What would be the difference between using more OR's and splitting the phrase into multiple searches?

jatar_k




msg:3538352
 2:20 pm on Jan 1, 2008 (gmt 0)

more ORs

you would have to add each word to your query, maybe something like this

$query = "SELECT * FROM site_data WHERE description LIKE '%" . $keywords[0] . "%' OR page_title LIKE '%" . $keywords[0] . "%' OR keywords LIKE '%" . $keywords[0] . "%' OR description LIKE '%" . $keywords[1] . "%' OR page_title LIKE '%" . $keywords[1] . "%' OR keywords LIKE '%" . $keywords[1] . "%'";

you would need to construct this in a loop so it added the right amount for the number of words entered

multiple queries would be something like

$query = "SELECT * FROM site_data WHERE description LIKE '%" . $keywords[0] . "%' OR page_title LIKE '%" . $keywords[0] . "%' OR keywords LIKE '%" . $keywords[0] . "%'";

then pull those results into an array then do

$query = "SELECT * FROM site_data WHERE description LIKE '%" . $keywords[1] . "%' OR page_title LIKE '%" . $keywords[1] . "%' OR keywords LIKE '%" . $keywords[1] . "%'";

and pull those results

you would again need to loop it to do a query for each word

you might be best doing a query with both to start
if that doesn't return enough results then split the phrase (if needed) and query for each word individually

brancook




msg:3538394
 4:28 pm on Jan 1, 2008 (gmt 0)

Thanks for the info I'll give it shoot.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved