gpt4 book ai didi

php - 在表格中显示购物车

转载 作者:行者123 更新时间:2023-11-30 23:21:00 25 4
gpt4 key购买 nike

我认为我完全错了,但我试图在将商品添加到购物车时将其值显示到表格中。我的代码如下所示:

<?php
function minicart() {
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));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
'<table border = "1">'
'<tr>'
'<td>'<echo $get_row['name'].' x '.$value.' @ &pound;'.number_format($get_row['price'], 2).' = &pound;'.number_format($sub, 2).' <a href="minicart.php?remove='.$id.'">[-]</a> <a href="minicart.php?add='.$id.'">[+]</a> <a href="minicart.php?delete='.$id.'">[Delete]</a><br />''</tr>';
'</tr>'
'</table>'
}
}
$total += $sub;
}
}
if ($total==0) {
echo "Your cart is empty";
}
else {
echo '<br />Total: &pound;'.number_format($total, 2);

?>

最佳答案

试试这个:

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>&pound;' . 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>&pound;' . number_format($total, 2) . '</td></tr></tfoot></table>';
echo implode( "\n", $tbl );

}
}

我更改了逻辑,如果商品数量为零,它会显示“空车”消息,允许零成本商品。

关于php - 在表格中显示购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15463393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com