我有这个循环运行的购物车脚本,但是当我尝试通过电子邮件发送它时,我只允许我通过电子邮件发送 9 件物品,超过 9 件物品的电子邮件只是空白。有人告诉我,我应该将我的查询更改'SELECT * FROM books WHERE id = '.$id;
为将返回所有项目而没有循环的查询。这是正确的,我应该怎么做?如果是这样,有人可以给我一个例子来说明我会怎么做吗?
<?php
function showCarts() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<table style="border-width:1px; bordercolor="#0099FF"">';
$output[] = '<tr>';
$output[] = '<thead bgcolor="#0099FF">';
$output[] = '<th>Item</th>';
$output[] = '<th>Price</th>';
$output[] = '<th>Quantity</th>';
$output[] = '<th>Total</th>';
$output[] = '</thead>';
$output[] = '</tr>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td>'.$title.' by '.$author.'</td>';
$output[] = '<td>$'.$price.'</td>';
$output[] = '<td>'.$qty.'</td>';
$output[] = '<td>$'.($price * $qty).'</td>';
$total += ($price * $qty);
$output[] = '</tr>';
}
$output[] = '</table>';
$tax = (.07);
$taxtotal += round($total * $tax,2);
$amounttotal += ($total + $taxtotal);
$output[] = '<p>Tax: <strong>$'.$taxtotal.'</strong></p>';
$output[] = '<p>Total: <strong>$'.$amounttotal.'</strong></p>';
}
return join('',$output);
}
?>