<?php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>订货单统计</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 50px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 12px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.total {
font-weight: bold;
color: #007bff;
}
</style>
</head>
<body>
<div class="container">
<h2>订货单统计</h2>
<table>
<tr>
<th>商品名称</th>
<th>产地</th>
<th>数量</th>
<th>单价(元)</th>
<th>总价(元)</th>
</tr>
<?php
// 定义数组,存储商品信息
$products = [
["name" => "主板", "origin" => "广东", "quantity" => 3, "price" => 379],
["name" => "显卡", "origin" => "上海", "quantity" => 2, "price" => 799],
["name" => "硬盘", "origin" => "北京", "quantity" => 5, "price" => 589]
];
// 初始化总价
$totalPrice = 0;
// 使用foreach遍历数组,并将其显示在表格中
foreach ($products as $product) {
$itemTotal = $product["quantity"] * $product["price"];
$totalPrice += $itemTotal;
echo "<tr>";
echo "<td>" . htmlspecialchars($product["name"]) . "</td>";
echo "<td>" . htmlspecialchars($product["origin"]) . "</td>";
echo "<td>" . htmlspecialchars($product["quantity"]) . "</td>";
echo "<td>" . htmlspecialchars($product["price"]) . "</td>";
echo "<td>" . htmlspecialchars($itemTotal) . "</td>";
echo "</tr>";
}
?>
<tr>
<td colspan="4" class="total">总计</td>
<td class="total"><?php echo htmlspecialchars($totalPrice); ?></td>
</tr>
</table>
</div>
</body>
</html>