<!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>&lt;?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 &lt; 1) {
	$current_page = 1;
 } elseif ($current_page &gt; $total_pages) {
	$current_page = $total_pages;
 }

// 计算起始订单和结束订单的索引
$start = ($current_page - 1) * $per_page;
$end = $start + $per_page - 1;

// 假设这里是从数据库中获取订单数据的代码
$orders = array();
for ($i = 1; $i &lt;= $total; $i++) {
	$orders[] = array(
		'order_id' =&gt; $i,
		'order_date' =&gt; date('Y-m-d H:i:s', time() - 3600 * 24 * $i),
		'customer_name' =&gt; 'Customer ' . $i,
		'total_amount' =&gt; rand(100, 1000),
	);
}

// 显示订单列表
echo '&lt;table&gt;';
echo '&lt;thead&gt;&lt;tr&gt;&lt;th&gt;订单号&lt;/th&gt;&lt;th&gt;下单时间&lt;/th&gt;&lt;th&gt;客户姓名&lt;/th&gt;&lt;th&gt;订单总金额&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;';
echo '&lt;tbody&gt;';
for ($i = $start; $i &lt;= $end &amp;&amp; $i &lt; $total; $i++) {
	echo '&lt;tr&gt;';
	echo '&lt;td&gt;' . $orders[$i]['order_id'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $orders[$i]['order_date'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $orders[$i]['customer_name'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $orders[$i]['total_amount'] . '&lt;/td&gt;';
	echo '&lt;/tr&gt;';
}
echo '&lt;/tbody&gt;';
echo '&lt;/table&gt;';

// 显示分页按钮
if ($total_pages &gt; 1) {
	echo '&lt;div&gt;';
	if ($current_page &gt; 1) {
		echo '&lt;a class=&quot;button&quot; href=&quot;?page=1&quot;&gt;首页&lt;/a&gt;';
		echo '&lt;a class=&quot;button&quot; href=&quot;?page=' . ($current_page - 1) . '&quot;&gt;上一页&lt;/a&gt;';
	}
	for ($i = max(1, $current_page - 3); $i &lt;= min($total_pages, $current_page + 3); $i++) {
		if ($i == $current_page) {
			echo '&lt;span class=&quot;button active&quot;&gt;' . $i . '&lt;/span&gt;';
		} else {
			echo '&lt;a class=&quot;button&quot; href=&quot;?page=' . $i . '&quot;&gt;' . $i . '&lt;/a&gt;';
		}
	}
	if ($current_page &lt; $total_pages) {
		echo '&lt;a class=&quot;button&quot; href=&quot;?page=' . ($current_page + 1) . '&quot;&gt;下一页&lt;/a&gt;';
		echo '&lt;a class=&quot;button&quot; href=&quot;?page=' . $total_pages . '&quot;&gt;尾页&lt;/a&gt;';
	}
	echo '&lt;/div&gt;';
}
?&gt;
</code></pre>
</body>
</html>

原文地址: https://www.cveoy.top/t/topic/mBni 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录