Radio Button Filter Not Working

Hi everyone,

the following page works, but I am having issues with the sidebar filters.
I want to try and filter by color and by date. If no filter is set then show all records.
I am using if/else statements along with the name/value pairs of the radio buttons.
Can anyone see what I am doing wrong? I have tried several things and this is what I have ended up with:
I have read many pages and tried using different online examples nothing seems to work:
Thanks in advance for any help :wink:

HTML / PHP Code

[code]<?php
//connect to the database
$dbc = mysqli_connect(‘host’, ‘user’, ‘pass’, ‘cars’) or die(‘Error connecting to MySQL Server.’);

//If RadioButton Clicked Sort the Database by dateadded Asc / Desc
if(isset($_POST[‘dateorder’])){
if($_POST[‘dateorder’] == ‘dateasc’){
//Run query for dateasc
$query = “SELECT * FROM cardetails ORDER BY caradded asc”;
}elseif($_POST[‘dateorder’] == ‘datedesc’){
//Run query for datedesc
$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[‘color’])){
if($_POST[‘color’] == ‘red’){
//Run query for red color
$query = “SELECT * FROM cardetails WHERE color = ‘red’”;
}elseif($_POST[‘color’] == ‘green’){
//Run query for green color
$query = “SELECT * FROM cardetails WHERE color = ‘green’”;
}elseif($_POST[‘color’] == ‘blue’){
//Run query for blue 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));

//Retrieve the practice tasks from the database table
$result = mysqli_query($dbc, $query) or die(‘Error querying database.’);

//start pagination
//Get the total count of rows.
$sql = “SELECT COUNT(id) FROM cardetails”;
$query = mysqli_query($dbc, $sql);
$row = mysqli_fetch_row($query);

//Here we have the total row count
$rows = $row[0];

//Number of results to show per page
$page_rows = 5;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//This makes sure last cannot be less than 1
if($last < 1){
$last = 1;
}
//establish the $pagenum variable
$pagenum = 1;

//Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET[‘pn’])){
$pagenum = preg_replace(’#[^0-9]#’, ‘’, $_GET[‘pn’]);
}

//This makes sure the page number isn’t below 1 or more than our $last page
if($pagenum < 1){
$pagenum = 1;
} else if($pagenum > $last) {
$pagenum = $last;
}
//This sets the range of rows to query for the chosen $pagenum
$limit = ‘LIMIT ’ . ($pagenum - 1) * $page_rows .’,’ .$page_rows;

//This is your query again, it is for grabbing just one page worth of rows by applying the limit variable
$sql = “SELECT id, caradded, make, color FROM cardetails ORDER BY id DESC $limit”;
$query = mysqli_query($dbc, $sql);

//$paginationCrls variable
$paginationCtrls = ’ ';

//if there is more than 1 page worth of results
if($last != 1){
/First we check if we are on page one. If we are then we don’t need a link to
the previous page or the first page so we do nothing. If we aren’t then we generate
links to the first page and to the previous page.
/

if($pagenum > 1){
	$previous = $pagenum - 1;
	$paginationCtrls .= '<a href="' . $_SERVER['PHP_SELF']. '?pn=' .$previous. '">&#171; Previous</a> &nbsp; &nbsp; ';
	//Render clickable number links that should appear on the left of the target page number
	for($i = $pagenum-4; $i < $pagenum; $i++){
		if($i > 0) {
			$paginationCtrls .= '<a href="' .$_SERVER['PHP_SELF']. '?pn=' . $i .'">'.$i.'</a> &nbsp; &nbsp; ';
		}
	}
}

//Render the target page number, but without it being a link
$paginationCtrls .= ‘’.$pagenum.’     ‘;
//Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ‘’.$i.’    ';
if($i >= $pagenum+4){
break;
}
}
//This adds the same as above only checking if if we are on the last page and then generating Next
if($pagenum != $last){
$next = $pagenum + 1;
$paginationCtrls .= ’     Next » ';
}
}
//Finish Pagination
?>

Basic Page Layout

Search / Filter Practice

Trial Filters

Filter by Date:

A - Z
Z - A


Filter by Colour:

Red
Green
Blue



My Database Records

<?php echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; while($row = mysqli_fetch_array($query)){ echo ""; echo ""; echo ""; echo ""; echo ""; } print '
Car Added OnMakeColor
". $row['caradded'] ."". $row['make'] ."". $row['color'] ."


';

//close connection to database
mysqli_close($dbc);
?>

<?php echo $paginationCtrls;?>

July / August 2016

[/code][/code]

MySQL Code

[code]CREATE DATABASE cars;

USE cars;

CREATE TABLE cardetails(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
caradded datetime NOT NULL DEFAULT NOW(),
make VARCHAR(50) NOT NULL,
color VARCHAR(50) NOT NULL);

DESCRIBE cardetails;

INSERT INTO cardetails(caradded, make, color) VALUES
(NOW(), “Toyota”, “Red”),
(NOW(), “VW”, “Green”),
(NOW(), “Nissan”, “Blue”),
(NOW(), “Toyota”, “Green”),
(NOW(), “Toyota”, “Green”),
(NOW(), “VW”, “Blue”),
(NOW(), “VW”, “Green”),
(NOW(), “Nissan”, “Blue”),
(NOW(), “Nissan”, “Green”);

SELECT * FROM cardetails;[/code]

CSS Code:

[code]body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 13px;
color:#333
}

p {
padding: 10px;
}

#wrapper {
margin: 0 auto;
width: 1000px;
}

#headerwrap {
width: 1000px;
float: left;
margin: 0 auto;
}

#header {
height: 75px;
background: #FFFFB0;
border-radius: 10px;
border: 1px solid #ebeb9c;
margin: 5px;
}

#contentwrap {
width: 720px;
float: left;
margin: 0 auto;
}

#content {
background: #FFFFFF;
border-radius: 10px;
border: 1px solid #ebebeb;
margin: 5px;
}

#pagination_controls {color: #3f3f3f; padding: 2% 0; font-size: 1em; text-align: center;}
#pagination_controls a:link, div#pagination_controls a:visited{
color: #3f3f3f;
padding: 4px 10px;
margin: 0 2px;
text-decoration: none;
background-color: #fff;
border: 1px solid #fff;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;}
#pagination_controls a:hover{
border: 1px solid #bcbcbc;
background-color: #bcbcbc;
color: #fff;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;}
#pagination_controls span.active{
padding: 4px 10px;
color: #fff;
border: 1px solid #48C8F5;
background-color: #48C8F5;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;}
#pagination_controls span.page{font-weight: bold; color: #bcbcbc;}

#leftcolumnwrap {
width: 275px;
float: left;
margin: 0 auto;
}

#leftcolumn {
background: #DEFFFE;
border-radius: 10px;
border: 1px solid #caebea;
margin: 5px;
padding: 5px;
}

#leftcolumn p{
font-weight: bold;
}

#footerwrap {
width: 1000px;
float: left;
margin: 0 auto;
clear: both;
}

#footer {
height: 40px;
background: #FFFFB0;
border-radius: 10px;
border: 1px solid #ebeb9c;
margin: 5px;
}

table{margin: 0 auto;}

th{
font-size: 1.25em;
font-weight: bold;
color: #444;
padding: 10px 8px;
border-bottom: 2px solid #444;}

td{
font-size: 1em;
border-bottom: 1px dashed #aadbd8;
color: #444;
padding: 15px 8px;}

tr:hover td{
color: #E3170D;}[/code]

You would have a lot less redundancy if you used prepared statements.

Don’t do this: action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
Leave the action blank, it will do the same thing.

Lines 35 and 38 are redundant


You are overwriting the result set. Section the code out, if x is the action do x Not do x then y as y will default you to the beginning.

Thanks astonecipher.

I took out the action and left it blank and fixed that error for lines 35/38. Thanks for pointing those out.
Unfortunately I know nothing about prepared statements, however, I hope to get to them at some point.
For now, I am just looking to see if I can get this working, as I read the code (messy as it is) I can’t see why it does not work and what I am missing.

Thanks, Andy :wink:

mysqli_ prepared statements.

It is the wrong train of thought to ‘just get it to work.’ It is far better to do it right the first time

The issue is your pagination is overwriting it from what I can tell.

Let’s say I want red.
[php]$query = “SELECT * FROM cardetails WHERE color = ‘red’”;
[/php]

That query runs. Now further down, but before I get the page back, this runs:
[php]$sql = “SELECT id, caradded, make, color FROM cardetails ORDER BY id DESC $limit”;
[/php]

What do you think happens?

Hi astonecipher,

Thanks for pointing that out. I reckon the code is badly coded in the first place. I should probably plan it out better first. I was thinking the second filter was overwriting the first. I should probably combine the date and color filters into one query statement.

Thanks, Andy :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service