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
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. '">« Previous</a> ';
//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> ';
}
}
}
//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
?>
Search / Filter Practice
Trial Filters
Filter by Date:
A - ZZ - A
Filter by Colour:
RedGreen
Blue
My Database Records
<?php echo "Car Added On | "; echo "Make | "; echo "Color | "; echo "
---|---|---|
". $row['caradded'] ." | "; echo "". $row['make'] ." | "; echo "". $row['color'] ." | "; echo "
';
//close connection to database
mysqli_close($dbc);
?>
<?php echo $paginationCtrls;?>
July / August 2016
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]