melting_dog Posted December 15, 2011 Share Posted December 15, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253223-more-advanced-way-to-search-for-like-names/ Share on other sites More sharing options...
QuickOldCar Posted December 15, 2011 Share Posted December 15, 2011 My best advice for you would be looking into doing fulltext searches Quote Link to comment https://forums.phpfreaks.com/topic/253223-more-advanced-way-to-search-for-like-names/#findComment-1298134 Share on other sites More sharing options...
QuickOldCar Posted December 15, 2011 Share Posted December 15, 2011 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)."); Quote Link to comment https://forums.phpfreaks.com/topic/253223-more-advanced-way-to-search-for-like-names/#findComment-1298160 Share on other sites More sharing options...
Psycho Posted December 15, 2011 Share Posted December 15, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/253223-more-advanced-way-to-search-for-like-names/#findComment-1298196 Share on other sites More sharing options...
melting_dog Posted December 19, 2011 Author Share Posted December 19, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/253223-more-advanced-way-to-search-for-like-names/#findComment-1299156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.