Pagination doesn't work.
Posted: Mon Jul 16, 2012 7:49 am
Hi,
I am working on a pagination system. What I have is a slightly modified version of the pagination tutorial. It displays 10 listings on the first page as it should but when I click on page links (2nd page, etc.)on the pagination, nothing happens. It doesn't give any error and I can't see what I am doing wrong. Here is all the code I have:
.htaccess
I am working on a pagination system. What I have is a slightly modified version of the pagination tutorial. It displays 10 listings on the first page as it should but when I click on page links (2nd page, etc.)on the pagination, nothing happens. It doesn't give any error and I can't see what I am doing wrong. Here is all the code I have:
.htaccess
RewriteEngine On RewriteRule ^category/([A-Za-z0-9-]+)$ category.php?category=$1 RewriteRule ^category/([A-Za-z0-9-]+)/([0-9]+)$ category.php?category=$1&?p=$2functions.php - I have three tables: listings, categories and listing_category
function get_listings($category, $page, $per_page) {
$category = mysql_real_escape_string(strip_tags(trim($category)));
$start = ($page - 1) * $per_page;
$end = $page * $per_page;
$listings = array();
$query = "SELECT `name`, `title` FROM `listings`
JOIN listing_category ON `lc_listing_id` = `listing_id`
JOIN categories ON `lc_category_id` = `category_id`
WHERE `category_name` = '$category'
ORDER BY `name` ASC LIMIT $start, $end";
$query = mysql_query($query);
while($row = mysql_fetch_assoc($query)) {
$listings[] = $row;
}
return $listings;
}
function get_total_listings($category) {
$query = mysql_query("SELECT COUNT(`listing_id`) FROM `listings`
JOIN listing_category ON `lc_listing_id` = `listing_id`
JOIN categories ON `lc_category_id` = `category_id`
WHERE `category_name` = '$category'");
return mysql_result($query, 0);
}
category.php<?php
require 'functions.php';
$category = get_category($_GET['category']);
$total_pages = ceil(get_total_listings($_GET['category']) / 10);
if (isset($_GET['p'])) {
if (($_GET['p'] >= 1) && ($_GET['p'] <= $total_pages)) {
$page = (int)$_GET['p'];
} else {
$page = 1;
}
} else {
$page = 1;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/style.css" />
<title><?php echo $category['category_title'] ?></title>
</head>
<body>
<?php foreach(get_listings($_GET['category'], $page, 10) as $listing) { ?>
<h1><?php echo $listing['listing_title'] ?></h1>
<?php } ?>
<div id="pagination">
<?php
for ($i = 1; $i <= $total_pages; ++$i) {
echo '<a href="/category/'.$_GET['category'].'/'.$i.'">'.$i.'</a>';
}
?>
</div>
</body>
</html>
Thanks for any ideas.