PDA

View Full Version : Hi just wanting to know how to search keywords


nanny
07-15-2007, 07:34 AM
I have the $_GET['SearchString'] for a text box and use it to search blah blah LIKE %SearchString% and repeat this for items in table to be searched.
However it will only return for the exact match in the database.
e.g. the category is hairdressing, if you place hairdressing salon, it gives no result but if you put in hair or hairdressing it does.
The table has about 15 items to search against any suggestions on how to get hairdressing + salon + book etc.
Thanks

davidj
07-15-2007, 11:03 AM
take the field data and explode it using a space this will turn the data into an array on each word. Then search the array using the search value.

nanny
07-17-2007, 01:05 AM
Do you mean something like this?

if (array_key_exists('Search', $_GET)) {
$SearchString = trim($_GET['SearchString']);
$SearchString = explode(" ", str_replace(array("'","+","-","*","~","\"","(",")","<",">","\\"),"",$SearchString));
}


But will that only give the results as say input:
hairdressing + salon
and the output:
hairdressing salon
But I still have the problem because:
1: hairdressing word is in the database
2: salon word is in the database
3: But put together haidressing salon isn't an exact match so it will not output a return.

Also should I be using double or single quotes.
I get confused between useage.
Thanks for your help.....

nanny
07-23-2007, 02:05 AM
Hi still need help with this please..............can't seem to get the mysql to search for word as individual words in database.
Thanks.....

davidj
07-23-2007, 06:46 AM
ok
where you have...

hairdressing salon

do you want it to return results for both hairdressing and salon

nanny
07-23-2007, 06:49 AM
Hi davidj
Yes that is correct.
Thanks.

davidj
07-23-2007, 07:58 AM
ok

first you need to catch your form field value and then detect if its a greater than 1 word search

example...


$search = $_POST['search'] //<< contains the search string hair salon

$searchArray = explode(" ", $search); //<< explode into array using space
$count = count($searchArray); //<< count words in the searchArray

if($count > 1){ //<< if more than 1 word use a foreach loop

foreach($searchArray as $values){ //<< foreach will loop through each word

$needle = $values;
$haystack = $ROW['database']; //<< database text to search on

$found = strstr($haystack,$needle);

if($found){

$result=$result.$ROW['database'];

}

}



}else{

$needle = $_POST['search']; //<< 1 word only no need to use array
$haystack = $ROW['database']; //<< database text to search on

$found = strstr($haystack,$needle);

if($found){

$result=$ROW['database'];

}

}

display $result;

nanny
07-24-2007, 12:58 AM
Thanks davidj
I understand your process but now I just need to work out how to put on the page that suits what I have.
Now this is what I have at the moment and yes I have modified Dreamweaver to suit, although I have changed it substantially.
My Search form is this:

<form action="search.php" method="get" name="search" class="searchform" id="search">
<p>
<label for="text">Keyword</label>
<input name="SearchString" type="text" class="text" id="SearchString" value="<?php echo trim($_GET['SearchString']); ?>" size="15" maxlength="50">
<input name="Search" type="submit" class="searchbutton" id="submit" value="search" />
<img src="/images/maglass.gif" alt="search" width="14" height="14" /><br />
Search By <a href="#">Category</a><br />
Advanced Search -<a href="#"> Location </a><a name="searcher" id="searcher"></a>
<input name="search" type="hidden" id="search" value="<?php echo $_GET['SearchString']; ?>">
</p>
</form>


I start error checking the form with:


$MM_flag="search";
if (isset($_GET[$MM_flag])) {
$SearchString = trim($_GET['SearchString']);
if (empty($SearchString)) {
$error['SearchString'] = 'Please place a keyword in searchbox!';
}
else{
$unwanted = array('/\+/', '/,/');
// replace + and commas with a space
$clean = preg_replace($unwanted, ' ', $SearchString);
// use space to create an array from the cleaned-up text
$clean = explode(' ', $clean);
// initialize an empty array
$cleanArray = array();
// loop through the array to reject empty elements
foreach ($clean as $item) {
// empty items return false, so add only if something is there
if ($item)
array_push($cleanArray, $item);
}
}
}


What I use for the empty string to show no results is:

<?php } while ($row_rssearch = mysql_fetch_assoc($rssearch)); ?>
<?php } // Show if recordset not empty ?>
<?php if ((!empty($SearchString)) && $totalRows_rssearch == 0) { // Show if recordset empty ?>

What I use for the not empty string to show results is:

<?php } // Show if recordset not empty ?>
<?php if ((!empty($SearchString)) && $totalRows_rssearch > 0) { // Show if recordset not empty ?>
Your Search Results for <strong><?php echo $_GET['SearchString']; ?></strong> are: Records <?php echo ($startRow_rssearch + 1) ?> to <?php echo min($startRow_rssearch + $maxRows_rssearch, $totalRows_rssearch) ?> of <?php echo $totalRows_rssearch ?>


My error:

<?php if (isset($error['SearchString'])) { ?>
<span class="errmsg1"><?php echo $error['SearchString']; ?><br />
</span>
<?php } ?>
So now I begin.............

nanny
07-24-2007, 01:16 AM
So is this the way?


$MM_flag="search";
if (isset($_GET[$MM_flag])) {
$SearchString = trim($_GET['SearchString']);
if (empty($SearchString)) {
$error['SearchString'] = 'Please place a keyword in searchbox!';
}
else {
$searchArray = explode(" ", $SearchString);
$count = count($searchArray);
if($count > 1){

foreach($searchArray as $values){
$needle = $values;
$haystack = $totalRows_rssearch; //<< database text to search on

$Searchstring = strstr($haystack,$needle);
}

}

}else if {

$needle = $_GET['SearchString'];
$haystack = $totalRows_rssearch;

$SearchString = strstr($haystack,$needle);


}




Then how to I incorporate my $unwanted when someone puts in a + etc.

Hope I get on the right track.

davidj
07-24-2007, 07:03 AM
nothing like battering my head at 8am

right [rolls sleeves up]...

dont let me see you use DW code ever again ($_GET[$MM_flag]). You are a compitent programmer so why would you use it.

Its like an italian chef serving frozen takeout lasagna.
////////////

i will look at your code today

nanny
07-25-2007, 01:04 AM
Thanks davidj
appreciate it.
If I do a print of the keywords yes it seems to show but the results show differently.

Cheers