The Problem
I am having trouble filtering my table, I have filtered it for product category but I am unsure how to do it when looking at numbers.
Expected Outcome
What I expect to see when the user presses the submit button is for the table to change to what ever it was that submitted. For example, in the drop down box there is an option for '0 - 1', this meaning at price range less than a pound. If I was to press this I would expect to see the table change, showing only products with a price less than 1 pound.
What the actual outcome is
At the moment I am getting different errors every time I try something new, I first attempted to copy the category option but that didn't work.
Could I do something like this?
For the drop down box
<form action="database.php" method="post">
<select name="price" id="price">
<option value="?">All Products</option>
<option value="?">Less than £1</option>
<option value="?">More than £1, Less than £5</option>
<option value="?">More than £5, Less than £10</option>
</select>
<input type="submit" name="submit" value="Search"/>
</form>
Would not know what to put for the value.
The SQL code
$'?' = pg_query("SELECT Foodtype, Manufacturer,
Description, Price FROM food WHERE Price = BETWEEN 0.00 AND 1.00");
$'?' = pg_query("SELECT Foodtype, Manufacturer,
Description, Price FROM food WHERE Price = BETWEEN 1.00 AND 5.00");
$'?' = pg_query("SELECT Foodtype, Manufacturer,
Description, Price FROM food WHERE Price = BETWEEN 5.00 AND 10.00");
Here is my current code for implement the table
<?php
$conn = pg_connect("host=hostname.com port=1234
dbname=******* user=guest password=********");
// Empty var that will be populated if the form is submitted
$where = '';
if (isset($_POST['submit'])) {
if (!empty($_POST['category'])) {
// Where conditional that will be used in the SQL query
$where = " WHERE Category = '".pg_escape_string($_POST['category'])."'";
}
}
$res = pg_query($conn, "SELECT Foodtype, Manufacturer, Description, Price
FROM food " . $where . " ORDER BY Category ASC");
echo "<table id=\"myTable\" border='1'>";
while ($a = pg_fetch_row($res)) {
echo "<tr>";
for ($j = 0; $j < pg_num_fields($res); $j++) {
echo "<td>" . $a[$j] . "</td>";
}
echo "<td><form id='cart' name='cart' method='POST' action='addToBasket.php'>
<input type='submit' name='Select' id='Select' value='Add To Basket'>
</form></td>";
echo "</tr>
";
}
echo "</table>
";
I will need another Where conditional
$where = " WHERE Price= '".pg_escape_string($_POST['price'])."'";
Is there a better way to do a filter for price range?