【问题标题】:How to continue numbered list in all next pages with pagination?如何通过分页在所有下一页中继续编号列表?
【发布时间】:2014-01-15 15:59:36
【问题描述】:

我正在尝试在有序列表中显示数据,并在 php 中进行分页。它在第一页上工作正常,但在下一页上,列表再次从 1 开始,而不是从上一个数字继续。

这是我的代码:

<?php

// find out how many rows are in the table
$sql01 = mysql_query("SELECT COUNT(*) FROM pm,pm_reply");

$r = mysql_fetch_row($sql01);
$numrows = $r[0];
$rowsperpage = 3;
$totalpages = ceil($numrows / $rowsperpage);


if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   $currentpage = (int) $_GET['currentpage'];
} else {
    // default page num
   $currentpage = 1;
}

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
    // set current page to last page
   $currentpage = $totalpages;
}


// if current page is less than first page...
if ($currentpage < 1) {
    // set current page to first page
   $currentpage = 1;
}

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
// while there are rows to be fetched...

$sql=mysql_query("select *from pm where send_to='$_GET[name]' limit $offset, $rowsperpage");
$sql2=mysql_query("select *from pm_reply where send_to='$_GET[name]' limit $offset, $rowsperpage");

echo "<ol>";
while($rows=mysql_fetch_assoc($sql)) {
    echo"<li>From: <a href='profile.php?name=$rows[send_by]'>$rows[send_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows[subject]'>$rows[subject]</a></li>"; 
    echo "<hr>";
    echo "<br>";
}

while($rows2=mysql_fetch_assoc($sql2)) {   
    echo"<li>From: <a href='profile.php?name=$rows2[replied_by]'>$rows2[replied_by]</a><br><br><a style='text-decoration:none; font-size:14pt;' href='view_pm.php?name=$_GET[name]&pm=$rows2[subject]'>$rows2[subject]</a></li>"; 
    echo "<hr>";
    echo "<br>";
}

echo "</ol>";
echo "</div>";

//End Div Content

echo "<div style='clear:both;'></div>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<div id='pagelinks'>";

/******  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='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=1'>First...</a> ";
    // get previous page num
    $prevpage = $currentpage - 1;
}

// 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 ($x == $currentpage) {
            // if we're on current page...
            // 'highlight' it but don't make a link
            echo " [<b>$x</b>] ";      
        } else {
            // if not current page...
            // make it a link
            echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=$x'>$x</a> ";
        }
    }
}


// if not on last page, show forward and last page links   
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;    
   echo " <a href='{$_SERVER['PHP_SELF']}?name=$_GET[name]&currentpage=$totalpages'>...Last</a> ";
}

【问题讨论】:

  • 你能清理一下代码吗?
  • 代码和语法清理

标签: php page-numbering


【解决方案1】:

只需将start="$offset" 添加到您的echo "&lt;ol&gt;"; 行。

echo "<ol start='$offset'>";

【讨论】:

  • 简短的解决方案,+1。
  • Thanx 但在第一页上,列表从 0 而不是 1 开始,在下一页上,它还显示上一页的最后计数。有什么解决办法吗?
  • 只需将偏移量加 1 即可...$start_from = $offset + 1; echo "&lt;ol start='$start_from'&gt;";
  • Thanx,现在它从 1 开始,但在下一页,它还显示上一页的最后一个数字。现在该怎么办?
猜你喜欢
  • 1970-01-01
  • 2021-06-13
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
相关资源
最近更新 更多