Php 购物车问题,表中错误显示的项目

Php 购物车问题,表中错误显示的项目,php,mysql,html-table,Php,Mysql,Html Table,将项目添加到购物车时,它将创建购物车,然后显示包含项目添加信息的表。只要添加一个新项,它就会在原始表的下方创建一个新表,并在列标题应作为新行添加时重新创建列标题,将列标题作为一个表共享。到目前为止,我的代码是: function minicart() { $items = 0; $tbl = array(); foreach($_SESSION as $name => $value) { if ($value > 0) {

将项目添加到购物车时,它将创建购物车,然后显示包含项目添加信息的表。只要添加一个新项,它就会在原始表的下方创建一个新表,并在列标题应作为新行添加时重新创建列标题,将列标题作为一个表共享。到目前为止,我的代码是:

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 );

    }
}
功能迷你车()
{
$items=0;
$tbl=数组();
foreach($\会话为$name=>$value)
{
如果($value>0){
如果(substr($name,0,5)='cart_u2;')
{
$id=substr($name,5,(strlen($name)-5));
$get=mysql\u query('SELECT id,name,price FROM products WHERE id='.mysql\u real\u escape\u string((int)$id));
$tbl[]=“”
“项目”
“数量”
“单价”
“小计”
“行动”
. ''
;
而($get\u row=mysql\u fetch\u assoc($get)){
$items++;
$sub=$get_行['price']*$value;
$tbl[]=“”
“.$get_row['name']”
“.$value.”
“£;”.number_格式($get_row['price'],2)。”
“$pound;”。数字格式($sub,2)。”
. ''
. '  '
. '  '
. ' '
. ''
. ''
;
}
$tbl[]='';
}
$total+=$sub;
}
}
如果($items==0)
{
回应“你的购物车是空的”;
}
其他的
{
$tbl[]=“”
“总计:”
“£;”。数字_格式($total,2)。”;
回波内爆(“\n”,$tbl);
}
}

有什么建议吗

html标记不匹配,如果只想创建一行,则不需要
。tfoot是相同的,如果再次添加它,表标题将重复

只将
部分添加到$tbl,这样应该可以工作