Jump to content

Recommended Posts

Hi guys,

 

I have a list of products and I want to create a way to display similar products when a user views a particular product. I am thinking about doing it a certain way but need some advice on how to make it happen. I think I would do it like:

 

Get the name of the existing product

explode() the name by space" " e.g (explode(" ",$productname));

place the reuslts in a query that looks like:

 

SELECT * FROM product WHERE name LIKE '%$arrayitem1% OR LIKE '%arrayitem2%  OR LIKE '%arrayitem3%''

 

Im pretty fuzzy on the whole explode() part. Can anyone please give me some advice or point me in the direction of a similar tutorial/piece of code?

 

Thanks heaps!

But here is a simple way to explode the search words and insert it into the query, you may also use AND versus the OR

 

$searchTerms = explode(' ', $productname);
$searchWords = array();
foreach ($searchTerms as $searchwords) {
    $searchwords = trim($searchwords);
    if (!empty($searchwords)) {
        $searchWord[] = "name LIKE '%$searchwords%'";
    }
}


$query2 = mysql_query("SELECT * FROM product WHERE ".implode(' OR ', $searchWord).");

But here is a simple way to explode the search words and insert it into the query, you may also use AND versus the OR

 

$searchTerms = explode(' ', $productname);
$searchWords = array();
foreach ($searchTerms as $searchwords) {
    $searchwords = trim($searchwords);
    if (!empty($searchwords)) {
        $searchWord[] = "name LIKE '%$searchwords%'";
    }
}


$query2 = mysql_query("SELECT * FROM product WHERE ".implode(' OR ', $searchWord).");

 

If you are exploding a name on spaces, using trim on the value would probably have no effect (unless the name has tabs in it). But, you should definitely use mysql_real_escape_string() as well as filter out empty values (in case the name had two or more successive spaces).

 

Some slight modifications that I would make:

$searchTerms = explode(' ', $productname);
$searchTerms = array_filter($searchTerms); //Remove empties
foreach ($searchTerms as &$term)
{
    $term = "name LIKE '%" . mysql_real_escape_string($term) . "%'";
}

$query = "SELECT * FROM product WHERE " . implode(' OR ', $searchWord);

Thanks heaps guys, that did the trick!

 

-if anyone else is copying this just a slight grammatical correction to mjdamatos great code:

 

$query = "SELECT * FROM product WHERE " . implode(' OR ', $searchTerms);

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.