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

    }
}

有什么建议么?

4

1 回答 1

0

html 标签不匹配,</table>如果您只想创建一行,则不需要。tfoot 是一样的,如果再添​​加,表格标题会重复。

仅将<tr>...</tr>部分添加到 $tbl,这应该可以工作。

于 2013-03-17T22:07:08.487 回答