Welcome to WebmasterWorld Guest from 35.175.120.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Tweaking search script

     
8:30 pm on Nov 28, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Hi i hope some one can help, i have a simple script that searchs a database and outputs paged results.
the problem is the results are a bit to vague,
im using LIKE %% and having problems with "example bin finding cabin and table finding adjustable portable etc etc.

i have tried using RLIKE [[:<:]] [[:>:]] i get great 1 word results but i get no results on 2 word searches.

<?php
// GET THE SEARCH VARIABLE FROM URL AND BREAK INTO MULTIPLE WORDS
$var = @$_GET['q'] ;
$SearchString = explode ( " ", $var) ;

// NUMBER OF ROWS TO RETURN
$limit=30;

// CHECK FOR EMPTY STRING AND DISPLAY A MESSAGE
if (count($SearchString) == 0)
{
echo "<p>Please enter a search</p>";
exit;
}

// PUT THE SEARCH TERMS BACK TOGETHER AS SEPARATE WORDS
$searchQuery = "" ;

for ($i = 0 ; $i < sizeof($SearchString) ; $i++)
{
$searchQuery .= "\"$SearchString[$i]\" ";
}
echo $searchQuery ;

// CHECK FOR A SEARCH PARAMETER
if (!isset($var))
{
echo "<p align=center>We dont seem to have a search parameter!</p>";
exit;
}

// CONNECT TO DATABASE
mysql_connect("localhost","#*$!#*$!xxx","xxxxxxxxx"); //(host, username, password)
// SPECIFY DATABASE
mysql_select_db("xxxxxxx") or die("Unable to select database"); //select which database we're using
// BUILD SQL QUERY, AND SPECIFY TABLE AND FIELD NAMES
$query = "SELECT * FROM products WHERE title LIKE $searchQuery ORDER BY price";

echo $query ;
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
$var2 = eregi_replace(" ", "+", $var);

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
<?php

// display what the person searched for
echo "<p align=left><h1>$var</h1></p>";

// begin to show results set
//echo "Results";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["title"];
$price = $row["price"];

*******Echo results here********

$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);

print "&nbsp;<a href=\"$prevs/$var2.html\">&lt;&lt;
Prev 30</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$news/$var2.html\">Next 30 &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
//echo "<p>$numrows</p>";

?>

Can anyone please point out any errors or suggestions, TIA

10:06 am on Nov 29, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


anyone please
12:27 pm on Nov 29, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


I have tried this code below but im missing something as searchquery 1 and 2 are coming up the sameword. the last word searched.

Can anyone help please

<?php
// GET THE SEARCH VARIABLE FROM URL AND BREAK INTO MULTIPLE WORDS
$var = @$_GET['q'] ;
$array = explode(" ", trim($var));
// NUMBER OF ROWS TO RETURN
$limit=30;

$n=(count($array));
$o=($n-1);

//echo $n;
//echo $o;

for ($i = 0 ; $i < $o ; $i++);
{
$searchQuery .= "\"%$array[$i]%\" or title like";
}
for ($k = $o ; $k < $n ; $k++);
{
$searchQuery2 .= "\"%$array[$i]%\"";
}
//echo $searchQuery;
//echo $searchQuery2;

// CONNECT TO DATABASE
mysql_connect("localhost","#*$!xx","#*$!xxxx"); //(host, username, password)
// SPECIFY DATABASE
mysql_select_db("xxxxxx") or die("Unable to select database"); //select which database we're using
// BUILD SQL QUERY, AND SPECIFY TABLE AND FIELD NAMES
$query = "SELECT * FROM products WHERE title like $searchQuery $searchQuery2 ORDER BY price";