我在我的网站上的几个页面上使用以下结果和分页和排序代码。它工作正常,除了在每种情况下,它留下1条记录,无论sql查询是什么。结果显示正常。如果有20个结果,则会显示20个结果中的20个。但是,页面上只显示19个结果。什么可能导致一个记录没有显示?
// Find out how many items are in the table
$total = $dbh->query("select
count(*)
FROM posts
LEFT JOIN wp_users ON posts.ID=wp_users.ID
WHERE posts.active=1")->fetchColumn();
// How many items to list per page
$limit = 8;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = get_query_var('page', 1);
$page = is_numeric($page) && $page > 0 ? $page : 1;
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
$sort_columns = array('id' => 'posts.SID', 'name' =>
'posts.story_name', 'category' =>
'posts.category');
$sort_column = isset($_GET['sort']) & array_key_exists($_GET['sort'],
$sort_columns) ? $sort_columns[$_GET['sort']] : 'stories.SID';
$sort = isset($_GET['sort']) & array_key_exists($_GET['sort'],
$sort_columns) ? $_GET['sort'] : 'id';
$direction = isset($_GET['direction']) && in_array($_GET['direction'],
array('asc', 'desc')) ? $_GET['direction'] : 'desc';
// The "back" link
$prevlink = ($page > 1) ? '<a href="?
page=1&sort='.$sort.'&direct='.$direction.'" title="First page">«
</a> <a href="?page=' . ($page - 1) .
'&sort='.$sort.'&direct='.$direction.'" title="Previous page">‹
</a>' : '<span class="disabled">«</span> <span
class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) .
'&sort='.$sort.'&direct='.$direction.'" title="Next page">›</a>
<a href="?page=' . $pages . '&sort='.$sort.'&direct='.$direction.'"
title="Last page">»</a>' : '<span class="disabled">›</span>
<span class="disabled">»</span>';
// Display the paging information
$results = $dbh->prepare("select
wp_users.ID AS user_id,
wp_users.ID,
wp_users.display_name,
posts.SID,
posts.post_name,
posts.category,
posts.created
FROM posts
LEFT JOIN wp_users ON posts.ID=wp_users.ID
WHERE posts.active = 1
ORDER BY {$sort_column} {$direction} LIMIT {$start}, {$limit}");
$results->execute();
$rows = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<?php if(sizeof($rows) > 0) : ?>
<style>
.table {
width: 100%;
}
</style>
<table class="table">
<tr>
<th><a href="?sort=id<?php if($sort == 'id' && $direction ==
'desc') echo '&direction=asc'; ?>">ID</a></th>
<th><a href="?sort=name<?php if($sort == 'name' && $direction ==
'desc') echo '&direction=asc'; ?>">Post Name</a></th>
<th><a href="?sort=category<?php if($sort == 'category' &&
$direction == 'desc') echo '&direction=asc'; ?>">Category</a></th>
</tr>
<?php foreach($rows as $row) : ?>
<tr>
<td><?php echo $row['SID']; ?></td>
<td><a href="http://example.com/posts?posted
=<?php echo $row[SID]; ?>"><?php echo stripslashes($row['post_name']); ?>
</a></td>
<td><?php echo $row['category']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?
// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, '
pages, displaying ', $start, '-', $end, ' of ', $total, ' results ',
$nextlink, ' </p></div>';