我还是一名新程序员。这不是我的日常工作,但我知道这很危险。我已成功地将嵌套数组插入到数据库中,但我无法弄清楚如何正确显示它。这是结帐过程中订购的商品清单。
这是回声的阵列:
Array
(
[products] => Array
(
[0] => Array
(
[name] => m10x1-5-001 IVB bolt
[code] => m10x1-5-001
[qty] => 1
[price] => 37.00
[image] => m10x1-5-001-S.jpg
[description] => This is a short description of my item.
)
[1] => Array
(
[name] => AA02-015-021 IVB bolt
[code] => AA02-015-021
[qty] => 1
[price] => 39.00
[image] => m10x1-5-001-S.jpg
[description] => I'm a classic bolt.
)
)
)
我需要以可读格式回应这个问题,因为它是在订单管理页面上进行的。它存储在mysql的'products'字段中序列化,这是我的初始代码无效:
if ($result->num_rows > 0) {
echo "<table border='1'><tr><th>ID</th><th>header1</th><th>header2:</th><th>Items:</th></tr>";
while($row = $result->fetch_assoc()) {
$decrypted = decrypt(base64_decode($row["number"]), ENCRYPTION_KEY);
$decrypted2 = decrypt(base64_decode($row["cvv"]), ENCRYPTION_KEY);
echo "<tr><td>".$row["id"]."</td><td>".$decrypted."</td><td>".$decrypted2."</td><td>";
$listitems = base64_decode(unserialize($row["products"]));
foreach($listitems as $item)
{
echo $item['name'] . $item['code'] . $item['qty'] . $item['price'] . $item['image'] . $item['description'] ;
}
echo "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
我知道这段代码还没有引用数组的嵌套特性。新手需要一些帮助。
答案 0 :(得分:0)
对于格式化输出,您可以打印HTML表格:
$listitems = base64_decode(unserialize($row["products"]));
$output = '<table>
<tr>
<th>Name</th>
<th>Code</th>
<th>Quantity</th>
<th>Price</th>
<th>Image</th>
<th>Description</th>
</tr>
';
foreach($listitems as $item)
{
$output .= '<tr>
<td>' . $item['name'] . '</td>
<td>' . $item['code'] . '</td>
<td>' . $item['qty'] . '</td>
<td>' . $item['price'] . '</td>
<td>' . $item['image'] . '</td>
<td>' . $item['description'] . '</td>
</tr>';
}
$output .= '</table>';
echo $output;