Jump to content

Using Explode() function in my search box


lebexpress

Recommended Posts

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

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.

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?  ??? ??

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.