将商品添加到购物车时,它会创建购物车,然后显示包含商品添加信息的表格。每当添加新项目时,它都会在原始项目下方创建一个新表,并在它们应该作为新行添加时重新创建列标题,将列标题共享为一个表。到目前为止我的代码:
function minicart()
{
$items = 0;
$tbl = array();
foreach($_SESSION as $name => $value)
{
if ($value > 0) {
if (substr($name, 0, 5)=='cart_')
{
$id = substr($name, 5, (strlen($name) -5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
$tbl[] = '<table border="1"><thead><tr>'
. '<th>Item</th>'
. '<th>Quantity</th>'
. '<th>Unit Price</th>'
. '<th>SubTotal</th>'
. '<th>Action</th>'
. '</tr></thead><tbody>'
;
while ($get_row = mysql_fetch_assoc($get)) {
$items++;
$sub = $get_row['price'] * $value;
$tbl[] = '<tr>'
. '<td>' . $get_row['name'] . '</td>'
. '<td>' . $value . '</td>'
. '<td>£' . number_format( $get_row['price'], 2 ) . '</td>'
. '<td>$pound;' . number_format( $sub, 2) . '</td>'
. '<td>'
. ' <a href="minicart.php?remove=' . $id . '">[-]</a> '
. ' <a href="minicart.php?add=' . $id . '">[+]</a> '
. ' <a href="minicart.php?delete=' . $id . '">[Delete]</a>'
. '</td>'
. '</tr>'
;
}
$tbl[] = '</tbody>';
}
$total += $sub;
}
}
if ($items==0)
{
echo "Your cart is empty";
}
else
{
$tbl[] = '<tfoot><tr>'
. '<td colspan="3" style="text-align:right; font-weight:bold">Total:</td>'
. '<td>£' . number_format($total, 2) . '</td></tr></tfoot></table>';
echo implode( "\n", $tbl );
}
}
有什么建议么?