如何使用分页在所有下一页中继续编号列表?

时间:2014-01-15 15:59:36

标签: php page-numbering

我正在尝试使用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> ";
}

1 个答案:

答案 0 :(得分:3)

只需将start="$offset"添加到echo "<ol>";行。

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