Page 1 of 1

Pagination doesn't work.

Posted: Mon Jul 16, 2012 7:49 am
by nyo
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
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=$2
functions.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.

Re: Pagination doesn't work.

Posted: Tue Jul 17, 2012 3:10 pm
by jacek
On the last line in your .htaccess file you have this
RewriteRule ^category/([A-Za-z0-9-]+)/([0-9]+)$ category.php?category=$1&?p=$2
There is an extra ? at the end of this line, it should be
RewriteRule ^category/([A-Za-z0-9-]+)/([0-9]+)$ category.php?category=$1&p=$2
I would try a
var_dump($_GET);
from your script to make sure you have the right data.

Re: Pagination doesn't work.

Posted: Thu Jul 19, 2012 12:04 pm
by nyo
Thank you very much Jacek, I guess I missed that question mark. Removing it fixed the issue. Now it works as it should.

Re: Pagination doesn't work.

Posted: Fri Jul 20, 2012 1:56 am
by jacek
Good good :D