Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的搜索结果有史以来只留下一条记录_Php_Mysql_Pagination - Fatal编程技术网

Php 我的搜索结果有史以来只留下一条记录

Php 我的搜索结果有史以来只留下一条记录,php,mysql,pagination,Php,Mysql,Pagination,我在我的网站的几个页面上使用以下结果和分页和排序代码。它工作得很好,只是在每种情况下,不管sql查询是什么,它都会保留1条记录。结果显示正常。如果有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 post

我在我的网站的几个页面上使用以下结果和分页和排序代码。它工作得很好,只是在每种情况下,不管sql查询是什么,它都会保留1条记录。结果显示正常。如果有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">&laquo;
 </a> <a href="?page=' . ($page - 1) .
 '&sort='.$sort.'&direct='.$direction.'" title="Previous page">&lsaquo;
 </a>' : '<span class="disabled">&laquo;</span> <span
 class="disabled">&lsaquo;</span>';

// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) .   
'&sort='.$sort.'&direct='.$direction.'" title="Next page">&rsaquo;</a> 
<a href="?page=' . $pages . '&sort='.$sort.'&direct='.$direction.'"
title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span>
<span class="disabled">&raquo;</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>';
//找出表中有多少项
$total=$dbh->query(“选择
计数(*)
发帖
左加入帖子上的wp_用户。ID=wp_用户.ID
其中posts.active=1“->fetchColumn();
//每页要列出多少项
$limit=8;
//有多少页
$pages=ceil($total/$limit);
//我们现在在哪一页?
$page=get\u query\u var('page',1);
$page=是否为数字($page)&&$page>0$页码:1;
//计算查询的偏移量
$offset=($page-1)*$limit;
//要向用户显示的一些信息
$start=$offset+1;
$end=min($offset+$limit),$total);
$sort\u columns=array('id'=>'posts.SID','name'=>
'posts.story_name','category'=>
"职位类别";;
$sort\u column=isset($\u GET['sort'])和数组\u key\u存在($\u GET['sort']),
$sort\u列)$排序列[$\u获取['sort']]:'stories.SID';
$sort=isset($\u GET['sort'])和数组\u key_存在($\u GET['sort']),
$sort\u列)$_获取['sort']:'id';
$direction=isset($\u GET['direction'])和&in_数组($\u GET['direction']),
数组('asc','desc'))$_获取['direction']:'desc';
//“反向”链接
$prevlink=($page>1)?“”:&拉阔&伊萨库;
//“向前”链接
$nextlink=($page<$pages)?'
“:”&rsaquo;
&",;
//显示分页信息
$results=$dbh->prepare(“选择
wp_users.ID作为用户ID,
wp_users.ID,
wp_users.display_name,
希德先生,
posts.post_名称,
职位类别,
posts.created
发帖
左加入帖子上的wp_用户。ID=wp_用户.ID
其中posts.active=1
按{$sort_column}{$direction}LIMIT{$start},{$LIMIT}”)排序;
$results->execute();
$rows=$results->fetchAll(PDO::FETCH_ASSOC);
?>
.桌子{
宽度:100%;
}

这将是一个极限问题,看看数学来计算它是的,谢谢。我就是想不出数学上有什么问题。我已经尝试了+1和其他一些方法,但我还没有弄明白。尝试更改
$offset=($page-1)*$limit至<代码>$offset=($page)*$limit。如果不起作用,请尝试
$start=$offset+1
$start=$offset。第一个选项不起作用,但当我更改$start=$offset;(没有+1)它确实给了我所有的结果。但是,当我这样做时,显示信息显示“结果0-10”,而不是显示“结果1-10”。我想我可以接受,但显然我更喜欢让它显示所有结果并正确显示页面信息。已更改$start=$offset,如您所建议,以获得正确的结果。但是,我还在显示代码之前添加了$start++,因此它显示“显示200个结果中的1-20个”,而不是“显示200个结果中的0-20个”