<?php
/**
* 封装分页函数
* $table [字符串] 表名
* @$size [数字][每页显示条数]
*/
function fenye($table, $size)
{
$link = mysqli_connect("127.0.0.1", "root", "123456", "18test") or die("数据库连接失败!");
$sql = "select * from $table";
$result = mysqli_query($link, $sql);
$total = mysqli_num_rows($result); //总条数
// $size = 5; //每页显示条数
$url = "pages.php"; //请求的URL
$p = isset($_GET["p"]) ? $_GET["p"] : 1; //当前页,默认第一页
$pages = ceil($total / $size); //总页数
$start = ($p - 1) * $size; //开始去数据的位置
$sql = "select * from $table limit $start,$size";
$result = mysqli_query($link, $sql);
$next = $p + 1 >= $pages ? $pages : $p + 1; //下一页
$prev = $p - 1 < 1 ? 1 : $p - 1; //前一页
echo '<table border="1">';
while ($row = mysqli_fetch_row($result)) {
echo '<tr>';
foreach ($row as $col) {
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo "<div><a href='?p=1'>首页</a>;
<a href='?p=$prev'>上一页</a>
<a href='?p=$next'>下一页</a>
<a href='?p=$pages'> 尾页</a>";
echo '</div>';
mysqli_close($link);
}