<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if (isset($_POST['searchquery']))
{
$suffix = "";
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
$bhp_min = $_POST['bhp_min'];
$bhp_max = $_POST['bhp_max'];
$car_colour = $_POST['colour'];
$car_power = $_POST['power'];
$price_min = $_POST['price_min'];
$price_max = $_POST['price_max'];
$bhp_search = false;
if ($bhp_min != "" && $bhp_max != "") $bhp_search = true;
$bhp_search_sql = "cars.bhp >=" . $bhp_min . " AND cars.bhp <= " . $bhp_max;
$price_search = false;
if ($price_min != "" && $price_max != "") $price_search = true;
$price_search_sql = "cars.price >=" . $price_min . " AND cars.price <= " . $price_max;
$text_search_sql = "(car_name LIKE '%$searchquery%' OR car_info LIKE '%$searchquery%')";
$car_colour_sql = "colours.colour LIKE '%$car_colour%'";
$car_power_sql = "power.type LIKE '%$car_power%'";
$sqlCommand = "SELECT cars.id, car_name, position, visible, car_info, cars.colour AS colour_id, link, bhp, picture, price, colours.colour FROM cars INNER JOIN colours ON cars.colour=colours.id INNER JOIN power ON cars.power=power.id";
$first_condition_added = false;
if ($bhp_search != "")
{
$sqlCommand .= " WHERE ";
$sqlCommand .= $bhp_search_sql;
$first_condition_added = true;
}
if ($searchquery != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $text_search_sql;
$first_condition_added = true;
}
if ($car_colour != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $car_colour_sql;
$first_condition_added = true;
}
if ($car_power != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $car_power_sql;
$first_condition_added = true;
}
if ($price_search != "")
{
if ($first_condition_added == false) $sqlCommand .= " WHERE ";
else
$sqlCommand .= " AND ";
$sqlCommand .= $price_search_sql;
$first_condition_added = true;
}
//echo $sqlCommand;
include_once("connect_to_mysql.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if ($count > 0)
{
if ($suffix = ($count !=1) ? 's' : '');
$search_output .= "<hr />$count result$suffix for <strong>$searchquery</strong><hr />";
while ($row = mysql_fetch_array($query))
{
// $id = $row["nid"];
// $title = $row["title"];
// $search_output .= "$id - $title";
$search_output .= "<a href=\"?q=node/{$row['link']}\">{$row['car_name']}(" . $row['colour'] . ") BHP : " . $row['bhp'] . " Price : " . $row['price'] . "</a><br />\n";
} // close while
}
else
{
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}
?>
<html>
<head>
</head>
<body>
<form action="
http://www.mytestweb.co.uk/?q=products" method="post">
Search cars:
<input name="searchquery" type="text" size="44" maxlength="88">
<br/>
Bhp between :
<input name="bhp_min" type="text" size="5" maxlength="5">
and
<input name="bhp_max" type="text" size="5" maxlength="5">
<br/>
Colour :
<input name="colour" type="text" size="20" maxlength="20">
<br/>
Power :
<input name="power" type="text" size="20" maxlength="20">
<br/>
Price between :
<input name="price_min" type="text" size="5" maxlength="5">
and
<input name="price_max" type="text" size="5" maxlength="5">
<input name="myBtn" type="submit">
</form>
<div>
<?php echo $search_output; ?>
<!--echo $search_output;-->
</div>
</body>
</html>