Jump to content

[SOLVED] pagination not working


soycharliente

Recommended Posts

I went through both of the tutorials from the main site and neither one of them actually shows enough pages or makes the numbers links.

Can anyone see what's wrong? I get just a number 1 at the bottom and it's not even a link. I have 21 threads and the limit is 10, so I should get at least 3 pages.

<?php
$page = (!isset($_GET["page"])) ? 1 : $_GET["page"];
$max_results = 10;
$from = ($page * $max_results) - $max_results;
dbconnect();
$q = "SELECT COUNT(*) FROM blog_threads";
$r = mysql_query($q);
$total_results = mysql_num_rows($r);
$total_pages = ceil($total_results / $max_results);

$query =  "SELECT * FROM blog_threads ORDER BY lastpostdate DESC LIMIT $from, $max_results";
$result = mysql_query($query) or DIE("Error: getThreads().");
if (mysql_num_rows($result) > 0) {
$c = 0;
echo "<table id=\"threads\" class=\"floatright w_100\" cellspacing=\"0\">";
echo "<tr><th class=\"w_25 topicHeader\">Subject</th><th class=\"w_25 topicHeader\">Started by</th><th class=\"w_25 topicHeader\">Posts</th><th class=\"w_25 topicHeader\">Last post</th></tr>";
while ($r = mysql_fetch_array($result)) {
	$c++;
	$id = $r["id"];
	$title = $r["title"];
	$created = $r["created"];
	$first = getUsername($r["firstposter"]);
	$last = getUsername($r["lastposter"]);
	$posts = getNumPosts($id);
	$datetime = explode(" ", $r["lastpostdate"]);
	$ymd = explode("-", $datetime[0]);
	$lastdate = date("d F Y", mktime(0, 0, 0, $ymd[1], $ymd[2], $ymd[0]))." at ".$datetime[1];
	echo ($c & 1) ? "<tr class=\"white\">\n" : "<tr class=\"bg_gray white\">\n";
	echo "<td><a href=\"showThread.php?id=$id\" title=\"$title\">$title</a></td>";
	echo "<td>$first</td><td>$posts</td><td class=\"smaller\">$lastdate<br />by $last</td>";
	echo "</tr>\n";
}
echo "</table>";

echo "<div id=\"pagination\">";

if ($page > 1){
	$prev = $page - 1;
	echo "<a href=\"".$_SERVER["PHP_SELF"]."?page=$prev\"><<Previous</a> ";
}

for ($i = 1; $i <= $total_pages; $i++){
	if ($page == $i){
		echo "$i ";
	} else {
		echo "<a href=\"".$_SERVER["PHP_SELF"]."?page=$i\">$i</a> ";
	}
}

if ($page < $total_pages){
	$next = $page + 1;
	echo "<a href=\"".$_SERVER["PHP_SELF"]."?page=$next\">Next>></a>";
}
echo "</div>";

} else {
echo "<p>There aren't any threads right now.</p>\n";
}
dbclose();
?>

Link to comment
https://forums.phpfreaks.com/topic/58768-solved-pagination-not-working/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.