未定义的变量:总计

时间:2013-04-16 13:46:39

标签: php

我遇到了这个问题

$total += $rows['price'] * $qty;

注意:未定义的变量:第42行的D:\ xampp \ htdocs \ WBPL-MusicLightDev \ inc \ functions.inc.php中的总数

function wbpl_showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',', $cart);
        //$contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        echo '<form action="index.php?page=cart&action=update" method="post" id="cart">';
        echo '<table border=0 align="center" class="table table-bordered">';


        foreach ($contents as $id => $qty) {
            $sql = "SELECT * from wbpl_product WHERE kd_product = '$id'";
            $result = mysql_query($sql) or die(mysql_error());
            $rows=mysql_fetch_array($result);
            //extract($row);

            echo    '<tr>
                        <td>Brand</td>
                        <td colspan="4">'. $rows['kd_product'] .'</td>
                    <tr>
                    <tr>
                        <td>Brand</td>
                        <td colspan="4">'. $rows['nama_brand'] .'</td>
                    <tr>
                    <tr>
                            <td>Instrument Type</td>
                            <td colspan="4">'. $rows['nama_instype'] .'</td>
                    </tr>
                    <tr">
                    <td rowspan="2">Price</td>
                    <td rowspan="2">Rp. ' . $rows['price'] . '</td>
                    <td rowspan="2"><input type="text" name="qty' . $id . '" value="' . $qty . '" size="2" maxlength="3" /></td>

                    <td rowspan="2">Rp. ' . ($rows['price'] * $qty) . '</td>

                    <td><a href="index.php?page=cart&action=delete&id=' . $id . '" class="btn btn-danger">Hapus</a></td>
                    </tr>
                    <tr><td><br></td></tr>';
            $total += $rows['price'] * $qty;
        }
        echo '</table>';
        $qty = getQty();


        echo '<p>Sub Total: <strong> Rp. ' . $total . '</strong></p>';



        //session_register('totalbayar');
        $_SESSION['totalbayar'] = $total;
        echo '<div><button type="submit" class="btn btn-primary">Update cart</button></div>';
        echo '</form>';
    } else {
        echo '<p>Keranjang belanja masih kosong.</p>';
    }
    //return join('', $output);
}

5 个答案:

答案 0 :(得分:2)

您无法向不存在的值添加内容; 第一个调用是:null + = $ rows ['price'] * $ qty; 这是不可能的,所以添加

  

$ total = 0;

在你的foreach循环之前

    function wbpl_showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',', $cart);
        //$contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        echo '<form action="index.php?page=cart&action=update" method="post" id="cart">';
        echo '<table border=0 align="center" class="table table-bordered">';

        $total = 0;
        foreach ($contents as $id => $qty) {
            $sql = "SELECT * from wbpl_product WHERE kd_product = '$id'";
            $result = mysql_query($sql) or die(mysql_error());
            $rows=mysql_fetch_array($result);
            //extract($row);

            echo    '<tr>
                        <td>Brand</td>
                        <td colspan="4">'. $rows['kd_product'] .'</td>
                    <tr>
                    <tr>
                        <td>Brand</td>
                        <td colspan="4">'. $rows['nama_brand'] .'</td>
                    <tr>
                    <tr>
                            <td>Instrument Type</td>
                            <td colspan="4">'. $rows['nama_instype'] .'</td>
                    </tr>
                    <tr">
                    <td rowspan="2">Price</td>
                    <td rowspan="2">Rp. ' . $rows['price'] . '</td>
                    <td rowspan="2"><input type="text" name="qty' . $id . '" value="' . $qty . '" size="2" maxlength="3" /></td>

                    <td rowspan="2">Rp. ' . ($rows['price'] * $qty) . '</td>

                    <td><a href="index.php?page=cart&action=delete&id=' . $id . '" class="btn btn-danger">Hapus</a></td>
                    </tr>
                    <tr><td><br></td></tr>';
            $total += $rows['price'] * $qty;
        }
        echo '</table>';
        $qty = getQty();


        echo '<p>Sub Total: <strong> Rp. ' . $total . '</strong></p>';



        //session_register('totalbayar');
        $_SESSION['totalbayar'] = $total;
        echo '<div><button type="submit" class="btn btn-primary">Update cart</button></div>';
        echo '</form>';
    } else {
        echo '<p>Keranjang belanja masih kosong.</p>';
    }
    //return join('', $output);
}

答案 1 :(得分:1)

声明$total变量:

$total = 0;

进入foreach-loop之前

问题是:

$total += ...;表示将...添加到$total的值,但是$total尚未在第一次循环中定义导致通知的循环。

答案 2 :(得分:0)

$total = 0;

并在使用$ total之前设置此将解决问题

答案 3 :(得分:0)

$x += y意味着首先读取变量值然后添加一些东西,然后(重新)分配结果。在foreach循环的第一次迭代期间,没有变量$total可以在执行+=运算符时读取,因此警告。
在循环之前初始化变量。

$total = 0;
foreach ($contents as $id => $qty) {
  [...]
  $total += something;

答案 4 :(得分:0)

要使用+=运算符,您必须已经定义了左侧变量。您可以通过在$total使用之前初始化$total += ...来解决此问题:

$total = 0;
foreach (...)
    $total += ...

这是因为$total += $x内部已转换为$total = $total + $x,因此您第一次使用它时,$total未定义。