分页帮助
|
*编辑***
通过将主要用于查询广播的电波查询与分页查询中的某些变量组合在一起,我取得了长足的进步,最终使分页工作。现在唯一的问题是第一页为空白,第二页是正确的,从结果41-80开始
这是限制和拉动电波的查询
$rowsperpage = 40;
$currentpage = (int) $_GET[\'currentpage\'];
$offset = ($currentpage - 1) * $rowsperpage;
$query = \"SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = \'active\' ORDER BY `date` DESC, `time` DESC LIMIT $offset, $rowsperpage\" ;
$request = mysql_query($query,$connection);
$counter = 0;
while($result = mysql_fetch_array($request)) {
这是分页代码:
$query = \"SELECT COUNT(*) FROM `CysticAirwaves`\";
$result = mysql_query($query, $connection) or trigger_error(\"SQL\", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 40;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET[\'currentpage\']) && is_numeric($_GET[\'currentpage\'])) {
// cast var as int
$currentpage = (int) $_GET[\'currentpage\'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$query2 = \"SELECT `id` FROM `CysticAirwaves` LIMIT $offset, $rowsperpage\";
$result = mysql_query($query2, $connection) or trigger_error(\"SQL\", E_USER_ERROR);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
// echo data
echo $list[\'id\'] . \" : \" . $list[\'number\'] . \"<br />\";
} // end while
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don\'t show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo \" <a href=\'http://www.cysticlife.org/Airwave_build.php?currentpage=1\'><<</a> \";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo \" <a href=\'http://www.cysticlife.org/Airwave_build.php?currentpage=$prevpage\'><</a> \";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it\'s a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we\'re on current page...
if ($x == $currentpage) {
// \'highlight\' it but don\'t make a link
echo \" [<b>$x</b>] \";
// if not current page...
} else {
// make it a link
echo \" <a href=\'http://www.cysticlife.org/Airwave_build.php?currentpage=$x\'>$x</a> \";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo \" <a href=\'http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage\'>></a> \";
// echo forward link for lastpage
echo \" <a href=\'http://www.cysticlife.org/Airwave_build.php?currentpage=$totalpages\'>>></a> \";
} // end if
/****** end build pagination links ******/
?>
长话短说,我只是无法获得前40个结果,但之后就可以了
没有找到相关结果
已邀请:
2 个回复
盛梯獭八搽
使用上面的示例查询选择要显示的数据,该查询将返回与查询相同的结果2 然后执行另一个SQL查询:
这将返回找到的行数而不使用限制。在这里查看更多: http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html 这将是管理分页的最快方法。 然后,您将循环浏览第一个查询的结果,然后显示。您需要跟踪的只是您所在的页面以及每页显示多少行。 如果页码* $ rowsperpage>第二选择的结果(FOUND_ROWS),则无需计算或显示\'next \'页面选项/链接。 对于“下一页”,循环循环3次以增加页码或下一页* $ rowsperpage>总行数。 对于\'previous \'页面,循环执行3次以减少页码或上一页= 1(在这种情况下中断循环)。 这应该够了吧。
伞腹
您需要处理所有这些if子句!阅读所有这些评论!那不是负担吗?
那个怎么样?如果您在理解它时遇到困难,可以在代码中添加一些注释来解释它,就像在代码中所做的一样-用您的话说,如果我的不合适,那只是一个建议。