I have an HTML input field from which the values are exploded and separated by each space in the string. This string then searches the database for matches and returns values if there is a match however I would like to search for multiple fields in one table from the original string.
Hopefully this will clear things up:
For example if the user searched for 'Sheldon boys jumper' I would like the database to search for a match form each of these keywords in each field of the database eg [school_name], [sex], [product_type]. At the moment I have this working for one field but I would like to return and gather the values for all three fields.
This is my code:
if (empty($_POST) === false) {
if(empty($_POST['title']) === true) {
$no_data = '<div class="alert alert-danger center">Please enter a title</div>';
} else {
$item_title = $_POST['title'];
$keywords = explode(" ", $item_title);
$query = "SELECT * FROM products WHERE ";
foreach($keywords as $keyword) {
$i++; // dump variable
if($i == 1) { $query .= "product LIKE '$keyword' "; }
else { $query .= "OR product LIKE '$keyword' "; }
}
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
echo $row['product'];
}
}