在桌子中显示购物车

时间:2013-03-17 16:48:28

标签: php mysql

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

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

    ?>

3 个答案:

答案 0 :(得分:0)

我尝试将表格重新编写为正确的格式:

echo '<table border = "1">';
while ($get_row = mysql_fetch_assoc($get)) {
    $sub = $get_row['price']*$value;
    echo '
    <tr>
    <td>'.
    $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 />
    </td>
    </tr>
    ';
}
echo '</table>';

请参阅PHP Strings

答案 1 :(得分:0)

我认为你想用以下内容替换if语句。

这将为您提供1个表,每个项目有一行,可能就是您要找的内容。

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

答案 2 :(得分: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 );

    }
}

我改变了逻辑,这样如果项目数为零,它就会显示“空车”信息,允许零费用项目。