<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }
$start_from = ($page-1) * 20;
$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20";
$rs_result = mysql_query ($sql,$connection);
?>
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><? echo $row["Name"]; ?></td>
<td><? echo $row["PhoneNumber"]; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT COUNT(Name) FROM students";
$rs_result = mysql_query($sql,$connection);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
?>
一切正常工作,但我想要像stackoverflow这样的结果,即第1、2、3、4、5 ...页的最后一页...和下一页
最佳答案
尝试这个!它对我来说很完美。
$page = $_REQUEST["page"];
if(!$page) $page = 1;
$limit = ($page - 1) * 20;
$sql = "SELECT * FROM students ORDER BY name ASC";
$rs_result = mysql_query ($sql,$connection);
$rows = mysql_num_rows($rs_result);
$a = $rows/20;
if ($a == floor($rows/20 * -1))
$page_= $a;
else
$page_ = floor($rows/20 * -1);
$page_ = $page_ * -1;
$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $limit, 20";
$rs_result = mysql_query ($sql,$connection);
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><?php echo $row["Name"]; ?></td>
<td><?php echo $row["PhoneNumber"]; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<?php
if($page_==0){
}else{
$b=$page+9;
$prev = $page-1;
$next = $page+1;
$first= 1;
$last= $page_;
echo "<td align=center>";
if ($page != 1)
echo " <a href='pagination.php?page=$first' style='text-decoration:NONE;'> First </a> ";
else
echo " First ";
if ($page != 1)
echo " <a href='pagination.php?page=$prev' style='text-decoration:NONE;'>Prev </a> ";
else
echo " Prev ";
echo "</td>";
for ($i=$page;$i<=$b;$i++) {
if ($page==$i){
echo "<td align=center>";
echo "$i";
echo"   ";
echo "</td>";
} else {
echo "<td align=center>";
echo "<a href='pagination.php?page=$i'>$i </a>";
echo"   ";
echo "</td>";
}
if($i==$page_)
break;
}
echo "<td align=center>";
echo"   ";
if ($page != $page_)
echo "<a href='pagination.php?page=$next' style='text-decoration:NONE;'>Next </a>";
else
echo "Next";
echo"   ";
if ($page != $page_)
echo "<a href='pagination.php?page=$last' style='text-decoration:NONE;'> Last </a>";
else
echo "Last";
echo "</td>";
}
?>
</tr>
</table>
关于php - 使用mysql分割页码选项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32112340/