【问题标题】:PHP - Undefined variable when sub total the price - Shopping cartPHP - 汇总价格时未定义的变量 - 购物车
【发布时间】:2014-08-02 02:03:44
【问题描述】:
<?php
session_start();

连接

mysql_connect ('localhost', 'Akram', '') or die (mysql_error());
mysql_select_db('cart'); 
$page=("index.php");

数量

if (isset($_GET['add'])){
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='. mysql_real_escape_string((int)$_GET['add']));
    while ($quantity_row = mysql_fetch_assoc($quantity)){
                if ($quantity_row['quantity']!=$_SESSION['cart_'.$_GET['add']]){
                    $_SESSION['cart_'.$_GET['add']]+='1';
        }   
    }
}

显示产品列表

function products (){
    $get = mysql_query('SELECT * FROM products');
            while ($row = mysql_fetch_assoc($get)){
                    echo '<p>' .$row ['name']. '</br>'.$row['price'].'</br>'.$row['quantity'].'<br/>'.$row['description']. '<br/>'.'<a href="cart.php?add='.$row['id'].'">'.'Add to cart'.'</a>'.'</p>';
    }
}

在购物车中显示产品

    function cart ()    {
        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;
                        echo $get_row['name']  .  ' X ' . $value . ' @ &pound;' . number_format($get_row['price'], 2) . ' = ' . '&pound'.number_format($sub, 2). '<a href="cart.php?remove='.$id.'">'.'[REMOVE]'.'</a>'. '<a href="cart.php?add='.$id.'">'.'[ADD]'.'</a>'. '<a href="cart.php?delete='.$id.'">'.'[DELETE]'.'</a>';
                    }       
                }
                    $total += $sub; //getting error Undefined variable
            }
        }
if ($total==0) { //getting error Undefined variable
    echo "your cart is empty!";
}
else {
    echo 'Total: &pound;'. number_format($total, 2);
    //PayPal Button here
}
}

添加、删除和删除按钮

if(isset($_GET['add'])){
    $_SESSION['cart_' .(int)$_GET['add']]+='1';
    header('Location: ' .$page);
}

if (isset($_GET['remove'])){
    $_SESSION['cart_' . (int)$_GET['remove']]--;
    header('Location: ' .$page);
}

if (isset($_GET['delete'])){
    $_SESSION['cart_' .(int)$_GET['delete']]='0';
    header('Location: ' .$page);
}


?>

我试图在将所有项目添加到存储桶后获取它们的总价格,但我遇到 $total var 未定义变量错误。

【问题讨论】:

  • SO 的一般提示。尽量避免像这样的大型代码转储。而是只显示您认为是相关代码的内容。如果您的展示不够,我们会要求更多。

标签: php arrays magento shopping-cart cart


【解决方案1】:

$total += $sub 放错位置有问题。

您应该将它移到 mysql_fetch 中,以便总数适用于查询中的每个项目。

while ($get_row = mysql_fetch_assoc($get))  {
    $sub = $get_row ['price']*$value;
    echo $get_row['name']  .  ' X ' . $value . ' @ &pound;' . number_format($get_row['price'], 2) . ' = ' . '&pound'.number_format($sub, 2). '<a href="cart.php?remove='.$id.'">'.'[REMOVE]'.'</a>'. '<a href="cart.php?add='.$id.'">'.'[ADD]'.'</a>'. '<a href="cart.php?delete='.$id.'">'.'[DELETE]'.'</a>';
    $total += $sub; //added it here
}

您还应该将$total 初始化为0,所以当查询中没有商品(即购物车中没有商品)时,它会说购物车中没有商品。

比如:

function cart () {
    $total = 0;

这将解决您稍后有未设置变量的问题:

if ($total==0) { //getting error Undefined variable
    echo "your cart is empty!";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多