Forum Moderators: coopster

Message Too Old, No Replies

Split up searched keywords

         

AlexLee

3:43 am on Jan 27, 2005 (gmt 0)

10+ Year Member



How do I split up a phrase the user had entered in a textfield and search for them individually?

For example, a user enters "wireless mouse" in the searcg engine.
How do I make it such that it would be %wireless% and %mouse% instead of %wireless mouse%?

WhosAWhata

6:16 am on Jan 27, 2005 (gmt 0)

10+ Year Member



theres probably a better way, but this will work

$string = "wireless mouse";
$s = explode(" ",$string);
foreach($s as $k => $v){
$new[$k] = '%' . $v . '%';
}
$final = implode(" and ",$new);

dreamcatcher

9:32 am on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is an alternative method:

1. Seperate each keyword by a space and build a search string:


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

if (!empty($keywords))
{

for ($i=0; $i<count($keywords); $i++)
{

if ($i)

{
$search_string .= "OR field LIKE '%" . $keywords[$i] . "%'";
}
else
{
$search_string .= "field LIKE '%" . $keywords[$i] . "%'";
}

$search_terms = "WHERE (" . $search_string . ")";

}
else
{
echo "Please enter some keywords!";
}

2. Query your database

$query = "SELECT FROM table $search_terms";
$result = mysql_query($query) or die(mysql_error());

AlexLee

1:14 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



I have no idea how to fit that line into my code. I put in in front of my code now. But I had no idea how to fit the $search_string in.

$sql = "SELECT * FROM productcatalog_products, productcatalog_category
WHERE productcatalog_products.product_category = productcatalog_category.category_id
AND product_description LIKE '%$keywords%' OR product_title LIKE '%$keywords%' OR product_manufacturer LIKE '%$keywords%'
GROUP BY 'product_number'";
$mysql_result=mysql_query($sql, $connection);
$num_rows=mysql_num_rows($mysql_result);

if($num_rows==0)
{
echo"No Such Product In Our Database!";
}
else
{
while($row=mysql_fetch_array($mysql_result))
{
$product_title = $row["product_title"];
$product_manufacturer = $row["product_manufacturer"];
$product_price = $row["product_price"];
$product_category = $row["product_category"];
$product_image = $row["product_image"];
$category_name = $row["category_name"];
}
}

dreamcatcher

5:30 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The loop need changing slightly as you are querying multiple rows.


for ($i=0; $i<count($keywords); $i++)
{

if ($i)

{
$search_string .= "OR product_description LIKE '%" . $keywords[$i] . "%' OR product_title LIKE '%" . $keywords[$i] . "%' OR product_manufacturer LIKE '%" . $keywords[$i] . "%' ";
}
else
{
$search_string .= "product_description LIKE '%" . $keywords[$i] . "%' OR product_title LIKE '%" . $keywords[$i] . "%' OR product_manufacturer LIKE '%" . $keywords[$i] . "%' ";
}

$search_terms = "WHERE productcatalog_products.product_category = productcatalog_category.category_id AND (" . $search_string . ")";

Then:


$sql = "SELECT * FROM productcatalog_products, productcatalog_category
$search_terms GROUP BY 'product_number'";

Think that should work ok.

AlexLee

2:00 am on Jan 28, 2005 (gmt 0)

10+ Year Member



Now I am getting this error.
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\FYP\search_result.php on line 28

I think it has to do with this.

$sql = "SELECT * FROM productcatalog_products, productcatalog_category WHERE productcatalog_products.product_category = productcatalog_category.category_id AND " .$search_string. " GROUP BY 'product_number'";
$mysql_result = mysql_query($sql, $connection);
$num_rows = mysql_num_rows($mysql_result);

jatar_k

3:45 am on Jan 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there is most likely an error in the sql statement, therefore when $mysql_result is used in num_rows it returns an error.

try it this way to trap the actual error coming from mysql itself

$mysql_result = mysql_query($sql, $connection) or die (mysql_error());

AlexLee

10:20 am on Jan 28, 2005 (gmt 0)

10+ Year Member



Thanks to everyone I solved my errors!