Hey eveybody,
I have a single page which links to a small database with 9 records relating to cars.
In the sidebar I have radio buttons with the option to filter by date (asc /desc) or by color (red, green, blue).
When a radio button is selected and submitted a message is echoed to tell me which radio button was selected.
The records displayed however, do nothing, they don't seem to get filtered.
Here is some of the code:
<?php
//connect to the database
$dbc = mysqli_connect('host', 'user', 'password', 'cars') or die('Error connecting to MySQL Server.');
//If RadioButton Clicked Sort the Database by dateadded Asc / Desc
if(isset($_POST['submit']) && isset($_POST['dateorder']) && !empty($_POST['dateorder'])){
if($_POST['dateorder'] == 'dateasc'){
//Run query for dateasc
echo "You have selected :".$_POST['dateorder'];
$query = "SELECT * FROM cardetails ORDER BY caradded asc";
}elseif($_POST['dateorder'] == 'datedesc'){
//Run query for datedesc
echo "You have selected :".$_POST['dateorder'];
$query = "SELECT * FROM cardetails ORDER BY caradded desc";
}
}else{
$query = "SELECT * FROM cardetails ORDER BY id asc";
}
//If RadioButton Clicked Sort the Database by Color Red, Green, Blue
if(isset($_POST['submit']) && isset($_POST['color']) && !empty($_POST['color'])){
if($_POST['color'] == 'red'){
//Run query for red color
echo "You have selected :".$_POST['color'];
$query = "SELECT * FROM cardetails WHERE color = 'red'";
}elseif($_POST['color'] == 'green'){
//Run query for green color
echo "You have selected :".$_POST['color'];
$query = "SELECT * FROM cardetails WHERE color = 'green'";
}elseif($_POST['color'] == 'blue'){
//Run query for blue color
echo "You have selected :".$_POST['color'];
$query = "SELECT * FROM cardetails WHERE color = 'blue'";
}
}else{
$query = "SELECT * FROM cardetails ORDER BY id asc";
}
$result = mysqli_query($dbc, $query) or die('Error Refreshing the page: ' . mysqli_error($dbc));
The form looks like this:
<div id="leftcolumnwrap">
<div id="leftcolumn">
<h2>Trial Filters</h2>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<p>Filter by Date:</p>
<input type="radio" name="dateorder" value="dateasc"><label for="dateasc">A - Z</label><br>
<input type="radio" name="dateorder" value="datedesc"><label for="datedesc">Z - A</label><br>
<br><hr>
<p>Filter by Colour:</p>
<input type="radio" name="color" value="red"><label for="red">Red</label><br>
<input type="radio" name="color" value="green"><label for="green">Green</label><br>
<input type="radio" name="color" value="blue"><label for="blue">Blue</label>
<br><br>
<input name="submit" value="Submit" type="submit">
<br><br></form>
</div>
</div>
Any ideas, what can I do to sort this? Thanks for any help in advance, Andy ;-)