table分页展示代码
时间: 2023-07-09 11:35:58 浏览: 51
以下是一个基于PHP和MySQL的简单的table分页展示代码:
```
<?php
//数据库连接
$con = mysqli_connect("localhost","username","password","database");
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//查询数据总量
$count_query = mysqli_query($con,"SELECT COUNT(*) FROM table_name");
$row = mysqli_fetch_array($count_query);
$total = $row[0];
//每页展示数量
$limit = 10;
//计算总页数
$pages = ceil($total/$limit);
//获取当前页码
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
//计算偏移量
$offset = ($page - 1) * $limit;
//查询数据
$sql = "SELECT * FROM table_name LIMIT $offset, $limit";
$result = mysqli_query($con, $sql);
//展示数据
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['field1']."</td>";
echo "<td>".$row['field2']."</td>";
//...
echo "</tr>";
}
echo "</table>";
//展示分页链接
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $offset+1, '-', min($offset+$limit, $total), ' of ', $total, ' results ', $nextlink, ' </p></div>';
?>
```
以上代码实现了基本的table分页展示功能,你可以根据自己的需求进行修改。