I have a form that user can enter following
Shape = Round, Triangle, Square, Rectangle, Rhombus, Kite, Decagon
Color = Red, Black, Green, Yellow, Blue, White
Size = 1, 2, 3 … 11
if (!empty($_POST["shape"])):
$shape = trim($_POST["shape"]);
else:
$shape = "";
endif;
if (!empty($_POST["color"])):
$color = strtolower(trim($_POST["color"]));
else:
$color = "";
endif;
if (!empty($_POST["size"])):
$size = trim($_POST["size"]);
else:
$size = "";
endif;
SQL = SELECT * FROM items WHERE item_shape = $shape && item_color = $color && item_size = $size
if (!empty($_POST["shape"])):
$shape = trim($_POST["shape"]);
else:
$shape = " Round, Triangle, Square, Rectangle, Rhombus, Kite, Decagon";
endif;
// changed all post parameters sane wat u did with shape
SQL = SELECT * FROM items WHERE item_shape in ($shape) && item_color in ($color) && item_size = ($size)
You can try something like this:
// add as many features as you like to filter in this array
$features = array();
if (!empty($_POST["shape"])):
$features['shape'] = trim($_POST["shape"]);
endif;
if (!empty($_POST["color"])):
$features['color'] = strtolower(trim($_POST["color"]));
endif;
if (!empty($_POST["size"])):
$features['size'] = trim($_POST["size"]);
endif;
$sql = "SELECT * FROM items";
// if there is any feature in the array
if ( ! is_null($features))
{
$i = 0;
$len = count($features);
// check each one of the features
foreach ($features as $feature => $feature_value) {
if ($i == 0)
{
// if the first item, use WHERE
$sql .= ' WHERE ';
} else
{
// else, use &&
$sql .= ' && ';
}
$sql .= 'item_' . $feature . ' = ' . $feature_item;
$i++;
}
}