历史订单页面代码:包含首页、尾页、上一页、下一页按钮及相邻页面显示
<!DOCTYPE html>
<html>
<head>
<title>历史订单</title>
<style>
/* 样式可以根据需要修改 */
.button {
display: inline-block;
padding: 10px;
border: 1px solid #ccc;
background-color: #f7f7f7;
cursor: pointer;
}
.active {
background-color: #ccc;
color: #fff;
}
</style>
</head>
<body>
<h1>历史订单</h1>
<pre><code><?php
// 假设有100个订单,每页显示10个
$total = 100;
$per_page = 10;
// 获取当前页码,默认为第一页
$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
// 计算总页数
$total_pages = ceil($total / $per_page);
// 确保当前页码不超出范围
if ($current_page < 1) {
$current_page = 1;
} elseif ($current_page > $total_pages) {
$current_page = $total_pages;
}
// 计算起始订单和结束订单的索引
$start = ($current_page - 1) * $per_page;
$end = $start + $per_page - 1;
// 假设这里是从数据库中获取订单数据的代码
$orders = array();
for ($i = 1; $i <= $total; $i++) {
$orders[] = array(
'order_id' => $i,
'order_date' => date('Y-m-d H:i:s', time() - 3600 * 24 * $i),
'customer_name' => 'Customer ' . $i,
'total_amount' => rand(100, 1000),
);
}
// 显示订单列表
echo '<table>';
echo '<thead><tr><th>订单号</th><th>下单时间</th><th>客户姓名</th><th>订单总金额</th></tr></thead>';
echo '<tbody>';
for ($i = $start; $i <= $end && $i < $total; $i++) {
echo '<tr>';
echo '<td>' . $orders[$i]['order_id'] . '</td>';
echo '<td>' . $orders[$i]['order_date'] . '</td>';
echo '<td>' . $orders[$i]['customer_name'] . '</td>';
echo '<td>' . $orders[$i]['total_amount'] . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
// 显示分页按钮
if ($total_pages > 1) {
echo '<div>';
if ($current_page > 1) {
echo '<a class="button" href="?page=1">首页</a>';
echo '<a class="button" href="?page=' . ($current_page - 1) . '">上一页</a>';
}
for ($i = max(1, $current_page - 3); $i <= min($total_pages, $current_page + 3); $i++) {
if ($i == $current_page) {
echo '<span class="button active">' . $i . '</span>';
} else {
echo '<a class="button" href="?page=' . $i . '">' . $i . '</a>';
}
}
if ($current_page < $total_pages) {
echo '<a class="button" href="?page=' . ($current_page + 1) . '">下一页</a>';
echo '<a class="button" href="?page=' . $total_pages . '">尾页</a>';
}
echo '</div>';
}
?>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/mBni 著作权归作者所有。请勿转载和采集!