Welcome to WebmasterWorld Guest from 54.147.20.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

explode function

can't explode keywords for search

     

brancook

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

5+ Year Member



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

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

5+ Year Member



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

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

brancook

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

5+ Year Member



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

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

5+ Year Member



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

jatar_k

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

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

5+ Year Member



Thanks for the info I'll give it shoot.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month