Portwolf Posted November 12, 2009 Share Posted November 12, 2009 Ok to start things off, I am doing a revamp on a custom PHP CMS and the client wants the search function to do the following: Lets say a user searches for "WIX 24061" the current search function will mark that as not found and forward to the notfound page but when i search for "24061" it pulls it up. So what I decided i would do is an explode to a var and use a LIKE SQL query. But it is not working. Heres a snippet of the code that will should be working but its not. $whereExtra = ''; if(!empty($_SESSION['search'])) { if(strpos($_SESSION['search'], ',') !== false) { $parts1 = explode(',', $_SESSION['search']); for($i = 0; $i < count($parts1); $i++){ $whereExtra .= " AND (cp.sku LIKE '%" . $parts1[$i] . "%' OR cp.description LIKE '%" . $parts1[$i] . "%') "; } } else { $whereExtra = " AND (cp.sku LIKE '%" . $_SESSION['search'] . "%' OR cp.description LIKE '%" . $_SESSION['search'] . "%') "; } I have also tried the following: $whereExtra = ''; if(!empty($_SESSION['search'])) { if(strpos($_SESSION['search'], ',') !== false) { $parts1 = explode(',', $_SESSION['search']); foreach($parts1 as $p) $whereExtra .= " AND (cp.sku LIKE '%" . $p . "%' OR cp.description LIKE '%" . $p . "%') "; } else { $whereExtra = " AND (cp.sku LIKE '%" . $_SESSION['search'] . "%' OR cp.description LIKE '%" . $_SESSION['search'] . "%') "; } } Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/181238-php-mysql-search-function/ Share on other sites More sharing options...
DavidAM Posted November 12, 2009 Share Posted November 12, 2009 Your search string "WIX 24061" does not have a comma in it, so the explode is not being executed. Shouldn't you change the strpos() and explode() to be looking for a space? if(strpos($_SESSION['search'], ' ') !== false) { $parts1 = explode(' ', $_SESSION['search']); Or did I misunderstand the issue? Quote Link to comment https://forums.phpfreaks.com/topic/181238-php-mysql-search-function/#findComment-956242 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.