问题
I am trying to display data in an ordered list with pagination in php. It is working fine on page one but on next page, the list starts again from 1 instead of continuing from previous number.
Here is my code:
<?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]¤tpage=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]¤tpage=$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]¤tpage=$totalpages'>...Last</a> ";
}
回答1:
Just add start="$offset"
to your echo "<ol>";
line.
echo "<ol start='$offset'>";
来源:https://stackoverflow.com/questions/21142176/how-to-continue-numbered-list-in-all-next-pages-with-pagination