I am working on building a PHP based filter that will narrow down a search based on criteria specified by drop downs in the HTML file. In the MySQL file I have a column for a image_url, client, and service. When the search is complete I would like to display the image, client name, and services.
Currently I have the PHP setup to seach based on one specific set of criteria. I would like for it to take into account the extra set of perameters. How can I do this? Thanks in advance!
This is what I have so far.
HTML
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Filter</title>
</head>
<body>
<br />
<form action="filter.php" method="post">
Search by:
<br />
Client:
<select name="client">
<option value="any">- Any -</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>
Services:
<select name="services">
<option value="any">- Any -</option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP
<?php
// declare sting variable and set value to link to index.html
// connect to database and select database 'directory'
$con = mysql_connect("localhost","username","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("filter", $con);
$name = 'name';
if (isset($_REQUEST['client']))
{
$name = trim($_REQUEST['client']);
} else {
echo "Sorry, no search criteria.";
exit;
}
// contains the string input by the user on the name.html page
$query = "SELECT * FROM filter WHERE client LIKE '".mysql_real_escape_string($client)."%'";
$result = mysql_query($query) or die( "Problem executing: ". $query . " " . mysql_error() );
// if no results are returned from the query, give error message and exit
if (mysql_num_rows($result) == 0) {
echo "Sorry, no matching results.";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo "<img src="<?php echo $row["image_url"]; ?>"/> ;
echo "<b>Client: </b>";
echo $row["client"] ;
echo " ";
echo "<b>Services: </b>";
echo $row["services"];
echo "<br>";
echo "<br>";
}
?>
If I understand the question correctly, you are after something like this:
Explanation
When you submit your form, your
$_POSTarray will look like this for example:Note that you do not want to use
$_REQUESTunless you understood the order in which it is populated.Ok, so your
$_POSTarray contains the criteria you want to add to your base query:I’m not sure why you used
LIKEwhen you are going to use the fixed values from the Select options, so let’s transform the$_POSTarray to a simpleWHEREcondition only. Since you only want the values the base query can accept, we need to do some filtering first:The
filter_input_arraywill pull the values for client and service from the ´$_POST´ array and check whether their values are integers. Thearray_filterfunction takes care of removing any elements for which the validation failed. For the example array above, the$criteriavariable will now containAs you can see, ‘any’ got stripped because it is not an integer. Cool, because now all you got to do is
array_walkitThis code looks much more complicated than it really is: if there is any values in
$criteria, we overwrite the values to columnname = value where value can only be a number. You’ll end up withAfter that we append those values to the
$selectby imploding each value withAND.Your
$selectvariable will then containIf your
$_POSTarray contained for example:the resulting
$selectwould be:If your
$_POSTarray contained two ‘any’ values,$selectwould just contain the base query.The code snippet above can easily be extended to contain additional criteria. You just need to add the allowed keys to the filter function. The remaining code works with an arbitrary amount of array values in
$criteriaas long as the criteria values are numeric.If your needs get more complex, for instance because you need to add
OR,LIKEandINin the condition, consider using a query builder instead of hand coding it (not that it would be difficult).