I am using the php script below to generate comments from mysql, and also paginating it at the same time. I have 19 rows in the database, and I have set 5 comments per page in the variable. so I should have 4 links i.e. [1] [2] [3] [4] . but i am only getting 3 links
. I do not get any error. even when I set per page to 2, I still get 3 links.
<?php
$per_page = 5;
$total_query = $query = mysql_query("SELECT COUNT(*) FROM comments ") or die (mysql_error());
$pages = ceil(mysql_result($total_query, 0) / $total_query);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page ;
$query = mysql_query("SELECT * FROM comments LIMIT $start, $per_page") or die (mysql_error());
while ($comment = mysql_fetch_assoc($query)) {
<?php echo $comment['owner'] ; ?>
<?php echo htmlspecialchars($comment['body']) ;?>
<?php $date = date_create($comment['created']);
echo date_format($date, 'F j, Y g:i a'); ?>
if ($pages >= 1 && $page <= $pages) {
for ($x = 1; $x<=$pages; $x++) {
echo ($x == $page) ? '<a href="http://127.0.0.1/page.php?page='.$x.'">' . $x
.'</a> </span>' : '<a href="http://127.0.0.1/page.php?page='.$x.'"> [' . $x .' ]</a> ';
}
}
?>
Your line:
is getting the result and dividing by…
$total_query, which should be$per_page.