Hi everyone!
I have been working on a category / subcategory navigation system for my ad selling / publishing website.
I am no PHP expert, but I learn well by example...OK now onto the business...
The navigation system uses PHP and MySQL DB..
The DB I am using for the categories and subcategories tables is the ad manager DB. Note: The categories / subcategory tables were created by me in the ad manager database.
The idea is to have 4 pages to the category / subcategory navigation. Let me explain
Page 1: Categories.PHP- self explanitory displays categories
Page 2: Subcategories.PHP- self explanitory displays subcategories associated with clicked category
Page 3: Zones.PHP- Will display a zone (ad manager category that has ads associated with that zone #)
Page 4: Ad.php - Basically a details page for the ad that was clicked on the zones.PHP page.
I will post the SQL files relating to the database. And also the PHP pages that I have so far...If you feel like customizing the code, please do it in a more simplified way, I don't like the PHP code too complicated. Try to follow./ Use what I have in my pages.
Edit: should I also upload the ad manager so you can test on local host? I can do that as well if need be.
This is what I have so far...Note, there might be some obvious mistakes on the zones.PHP page. The cats and subcats pages do work, but maybe some cosmetic coding changes are needed?Like the variable names being passed could be called different Names? Haven't started the ad.PHP page yet.
Categories.php
PHP Code:
<html>
<head><title>Reunite My Site - Category Navigation Page</title>
<link rel="stylesheet" type="text/css" href="/css/index.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/pagination.css" media="screen" />
</head>
<style type="text/css">
body {
color: #FFF;
}
#main_content {
background-color: #292929;
border: 1px solid #000;
color: #FFF;
padding: 5px;
}
H3 {
color: #FFF;
}
#tpad {
padding-left: 20px;
}
</style>
<body>
<div id="main_content">
<div class="pagination white">
<div align="center"><h3>Category Selection</h3>
<!-- PHP CODE STARTS BELOW !-->
<?php
// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors','off');
?>
<?php
// Pagination Code
/* Include your code to connect to DB. */
include('connect.php');
/* Your DB table name */
$tbl_name="categories";
// How many adjacent pages should be shown on each side?
$adjacents = 1;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "categories.php"; //your file name (the name of this file)
$limit = 8; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = "SELECT DISTINCT * FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
/* Setup page vars for display. */
if ($page == 0) $page = 1; // if no page var is given, default to 1.
$prev = $page - 1; // previous page is page - 1
$next = $page + 1; // next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\" class=\"current\"> previous </a>";
else
$pagination.= "<span class=\"disabled\"> previous </span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter </span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</ a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\" class=\"number current\"> next </a>";
else
$pagination.= "<span class=\"disabled\"> next </span>";
$pagination.= "</div>\n";
}
// End of Pagination Script //
?>
<?php
// Start of Multiple Columns Script //
$cols=4; // Here we define the number of columns
echo "<table>"; // The container table with $cols columns
do {
echo "<tr>";
for($i=1;$i<=$cols;$i++) { // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysql_fetch_array($result);
if($row){
$cat_img = $row['cat_image_url'];
?>
<td>
<table>
<!-- columns can have both text and images -->
<td>
<a href="subcategories.php?id=<?php echo $row['cid'];?>"> <img src="<?php echo $cat_img ?>" /></a>
</td>
<p>
<tr>
<td align="center"><?php echo $row['name']; ?></td>
</tr>
<td>
<!-- Create gap between columns -->
<td width="60"> </td>
</tr>
</table>
</td>
<?
}
else{
// echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";
// End of Multiple Columns Script //
echo $pagination
?>
<?php
// Display the total number of categories.
$query = mysql_query("SELECT null FROM categories");
$count = mysql_num_rows($query);
echo "<p />";
echo "There are currently" ." " .$count ." " ."categories in the RMS-AD Display Network!";
?>
<div align="right">Copyright © 2012 - 2076</div>
</body>
</html>
Subcategories.php
PHP Code:
<html>
<head><title>Reunite My Site - Category Navigation Page</title>
<link rel="stylesheet" type="text/css" href="/css/index.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/pagination.css" media="screen" />
</head>
<style type="text/css">
body {
color: #fff;
}
#main_content {
background-color: #292929;
border: 1px solid #000;
color: #fff;
padding: 5px;
}
</style>
<body>
<div id="main_content">
<div class="pagination white">
<?php
// Passed Variable from categories.php
$subcat = $_GET['id'];
// Turn on error reporting
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors','off');
?>
<?php
// Pagination Code
/* Include your code to connect to DB. */
include('connect.php');
/* Your DB table name */
$tbl_name="subcategories";
// How many adjacent pages should be shown on each side?
$adjacents = 1;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT DISTINCT COUNT(*) as num FROM $tbl_name where sid = $subcat";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "subcategories.php"; //your file name (the name of this file)
$limit = 1; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = "SELECT DISTINCT * FROM $tbl_name where sid = ".(int)$subcat." LIMIT $start, $limit";
// exit($sql);
$result = mysql_query($sql) or die(mysql_error());
/* Setup page vars for display. */
if ($page == 0) $page = 1; // if no page var is given, default to 1.
$prev = $page - 1; // previous page is page - 1
$next = $page + 1; // next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev&id=$subcat\" class=\"current\"> previous </a>";
else
$pagination.= "<span class=\"disabled\"> previous </span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter&id=$subcat\" class=\"number current\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter </span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</ a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next&id=$subcat\" class=\"number current\"> next </a>";
else
$pagination.= "<span class=\"disabled\"> next </span>";
$pagination.= "</div>\n";
}
// End of Pagination Script //
?>
<?php
// Start of Multiple Columns Script //
$cols=4; // Here we define the number of columns
echo "<table>"; // The container table with $cols columns
do {
echo "<tr>";
for($i=1;$i<=$cols;$i++) { // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysql_fetch_array($result);
if($row) {
$img = $row['subcat_image_url'];
?>
<td>
<table>
<tr valign="top">
<td><a href="zones.php?zone=<?php echo $subcat; ?>"> <img src="<?php echo $img ?>" /></td>
<p>
<tr><td align="center"><?php echo $row['name']; ?></td></tr>
<!-- Create gap between columns -->
<td width="50"> </td>
</tr>
</table>
</td>
<?
}
else{
// echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";
// End of Multiple Columns Script //
echo $pagination
?>
<?php
// Display the total number of categories.
$q2 = mysql_query("SELECT DISTINCT * FROM subcategories where sid = ".(int)$subcat."");
$count = mysql_num_rows($q2);
// Start Subcategories Counter
if ( $count == 1 ) {
echo "<div align=\"center\">";
echo "<br />";
echo "There is currently Only" ." " .$count ." " ."Subcategory in this Category!";
echo "</div>";
} else {
echo "<div align=\"center\">";
echo "<br />";
echo "There are currently" ." " .$count ." " ."Subcategories in this Category!";
echo "</div>";
}
// End Subcategories Counter
?>
<div align="right">Copyright © 2012 - 2076</div>
</body>
</html>
zones.php
PHP Code:
<html>
<head><title>Reunite My Site - Category Navigation Page</title>
<link rel="stylesheet" type="text/css" href="/css/index.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/pagination.css" media="screen" />
</head>
<style type="text/css">
body {
color: #fff;
}
#main_content {
background-color: #292929;
border: 1px solid #000;
color: #fff;
padding: 5px;
}
</style>
<body>
<div id="main_content">
<div class="pagination white">
<div align="center"><h4>Zone Output Page (in development)</h4></div>
<?php
// Passed Variable from categories.php
$type = $_GET['ad_type'];
// Turn on error reporting
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors','on');
?>
<?php
// Pagination Code
/* Include your code to connect to DB. */
include('connect.php');
/* Your DB table name */
$tbl_name="amp_ads";
// How many adjacent pages should be shown on each side?
$adjacents = 1;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT DISTINCT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "zones.php"; //your file name (the name of this file)
$limit = 1; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = "SELECT * FROM $tbl_name, subcategories WHERE subcategories.sid = ".(int)$type." LIMIT $start, $limit";
// exit($sql);
$result = mysql_query($sql) or die(mysql_error());
/* Setup page vars for display. */
if ($page == 0) $page = 1; // if no page var is given, default to 1.
$prev = $page - 1; // previous page is page - 1
$next = $page + 1; // next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev&id=$zone\" class=\"current\"> previous </a>";
else
$pagination.= "<span class=\"disabled\"> previous </span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter&id=$zone\" class=\"number current\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter </span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</ a>";
}
$pagination.= "<span class=\"dots\">...</span>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>";
$pagination.= "<span class=\"dots\">...</span>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"number\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next&id=$zone\" class=\"number current\"> next </a>";
else
$pagination.= "<span class=\"disabled\"> next </span>";
$pagination.= "</div>\n";
}
// End of Pagination Script //
?>
<?php
// Start of Multiple Columns Script //
$cols=1; // Here we define the number of columns
echo "<table>"; // The container table with $cols columns
do {
echo "<tr>";
for($i=1;$i<=$cols;$i++) { // All the rows will have $cols columns even if
// the records are less than $cols
$row=mysql_fetch_array($result);
if($row) {
$img = $row['banner'];
?>
<td>
<table>
<tr valign="top">
<?php echo $img ?>
<td><img src="<?php echo $img ?>" /></td>
<p>
<tr><td align="center"><?php echo $row['name']; ?></td></tr>
<!-- Create gap between columns -->
<td width="50"> </td>
</tr>
</table>
</td>
<?
}
else{
// echo "<td> </td>"; //If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";
// End of Multiple Columns Script //
echo $pagination
?>
<?php
// Display the total number of categories.
$q2 = mysql_query("SELECT DISTINCT * FROM amp_ads");
$count = mysql_num_rows($q2);
// Start Subcategories Counter
if ( $count == 1 ) {
echo "<p />";
echo "There is currently Only" ." " .$count ." " ."Ads in this Category!";
} else {
echo "<p />";
echo "There are currently" ." " .$count ." " ."Ads in this Category!";
}
// End Subcategories Counter
?>
<div align="right">Copyright © 2012 - 2076</div>
<p />
</div>
</body>
</html>
Thank you all who help!
Brian