lebexpress Posted August 4, 2008 Share Posted August 4, 2008 Hello, I am trying to use the explode function in my search box to get accurate search results when customers try to search my inventory: I have: $search=($_POST['search']); //this will get the customers' input . . how can i use the explode function so I can place it in my query? $query="SELECT * FROM products WHERE (product_title) LIKE '%$search%' OR (p_model) LIKE '%$search%' OR (description_full) LIKE '%$search%'"; Thanks! Link to comment https://forums.phpfreaks.com/topic/118055-using-explode-function-in-my-search-box/ Share on other sites More sharing options...
JonnyThunder Posted August 4, 2008 Share Posted August 4, 2008 Something like... // Array to hold search terms (delimited by space) $inputarray = explode(" ", $_POST['search']); // ** Validate the inputs here to avoid SQL injection attacks! // Setup a blank array $likearray = array(); // Go through all the individual search terms foreach($inputarray as $currentterm) { $likearray[] = "(product_title) LIKE '$currentterm'"; $likearray[] = "(p_model) LIKE '$currentterm'"; $likearray[] = "(description_full) LIKE '$currentterm'"; } // Setup the SQL query $sql = "SELECT * FROM products WHERE" . implode(" OR ", $likearray); This is untested, but is roughly what you could use. Link to comment https://forums.phpfreaks.com/topic/118055-using-explode-function-in-my-search-box/#findComment-607331 Share on other sites More sharing options...
lebexpress Posted August 5, 2008 Author Share Posted August 5, 2008 First, thank you for your reply. I did the steps you told me, it partially worked, in other words, One of the title I have is: Hp Pavilion Notebook If I input HP in the search box, or HP Pavilion, it shows me results, but if I skip the Pavilion and search for HP Notebook, I do not get any results? ??? ?? Link to comment https://forums.phpfreaks.com/topic/118055-using-explode-function-in-my-search-box/#findComment-608127 Share on other sites More sharing options...
moselkady Posted August 5, 2008 Share Posted August 5, 2008 Have you checked for errors after your mysql_query using mysql_error()? Maybe you can print the query as well Link to comment https://forums.phpfreaks.com/topic/118055-using-explode-function-in-my-search-box/#findComment-608162 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.