我正在尝试创建一个分页,其中一次有5个项目,具体取决于DB中的项目数。我写了这段代码。但是我不知道如何更进一步。越野..这个更好的分页或改变
<?php
$myresult .= "<div class='pagination' >";
if ($pagenum == 1)
{
}
else
{
$pagenum = 1;
$myresult .= "<a href='javascript:newPage(\"".$pagenum."\")'> first </a>";
$myresult .= " ";
$previous = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous."\")'> Prev </a>";
if ($pagenum == $last)
{
$previous3 = $pagenum-4;
$myresult .= "<a href='javascript:newPage(\"".$previous3."\")'> $previous3 </a>";
$previous2 = $pagenum-3;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
}
if ($pagenum > 2)
{
$previous1 = $pagenum-2;
$myresult .= "<a href='javascript:newPage(\"".$previous1."\")'> $previous1 </a>";
}
if ($pagenum > 1)
{
$previous2 = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
$myresult .= " ";
}
}
$myresult .= "<span class=\"disabled\"> $pagenum </span>";
if ($pagenum == $last)
{
}
else {
if($pagenum < $last - 1)
{
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> $next </a>";
}
if($pagenum < $last - 2)
{
$next1 = $pagenum+2;
$myresult .= "<a href='javascript:newPage(\"".$next1."\")'> $next1 </a>";
}
if($pagenum == 1 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
$next3 = $pagenum+4;
$myresult .= "<a href='javascript:newPage(\"".$next3."\")'> $next3 </a>";
}
if($pagenum == 2 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
}
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> Next </a>";
$myresult .= "<a href='javascript:newPage(\"".$last."\")'> Last</a>";
}
$myresult .= "</div>";
$myresult .= "</br>";
?>
答案 0 :(得分:2)
也许不是最好的答案,但这是我用过的东西:
<?php
class Pagination {
function __construct() {
}
function getPaginatinationNavigation($page = 1, $num_rows, $rows_per_page, $page_name)
{
$lastpage = ceil($num_rows/$rows_per_page);
$page = (int)$page;
if ($page > $lastpage)
{
$page = $lastpage;
}
if ($page < 1) {
$page = 1;
}
$content='<p style="text-align: center;">';
if ($page == 1) {
$content.= " FIRST PREV ";
} else {
$content.= " <a href='$page_name?page=1'>FIRST</a> ";
$prevpage = $page-1;
$content.= " <a href='$page_name?page=$prevpage'>PREV</a> ";
}
$content.= " ( Page $page of $lastpage ) ";
if ($page == $lastpage) {
$content.= " NEXT LAST ";
} else {
$nextpage = $page+1;
$content.= " <a href='$page_name?page=$nextpage'>NEXT</a> ";
$content.= " <a href='$page_name?page=$lastpage'>LAST</a> ";
}
$content.= '</p>';
return $content;
}
}
?>
导入对象
require_once('classes/Pagination.php');
获取总行数:
$query= "SELECT COUNT(*) FROM TABLE_NAME";
$row = mysql_fetch_array($getResults);
$numRecords = $row[0];
使LIMIT根据页码限制行:
$limit = ' LIMIT ' . ($page - 1) * 25 .', 25';
查询使用限制
$query = 'SELECT * FROM TABLE_NAME' . $limit;
$getResults=mysql_query($query) or die(mysql_error());
像往常一样显示结果:
while($row = mysql_fetch_array($getResults))
{
DISPLAY RESULTS HERE
}
使用Class吐出导航:
$pagination->getPaginatinationNavigation($page, $numRecords, 25, 'display_page_name.php');
总的来说,对于分页,您需要进行2次查询:
1)获取搜索记录的总数 2)使记录限制在范围内的那些项目:例如25到50或1000到1050
您显示来自有限查询的记录,并转到下一组记录,您将页码增加一个。
希望这会有所帮助。不要忘记从网址查询字符串中清除任何数据。
如果您希望我进一步解释,请告诉我。