0

I'm making a PHP shopping cart and i'm trying to add the total price. When i want to use $total i get this error: Undefined variable: total. Why doesn't this work? How can i get this to work?

Code:

function cart() {
include 'dbh.inc.php';
foreach($_SESSION as $name => $value) {
    if($value > 0) {
        if(substr($name, 0,5)=='cart_') {
            $id = substr($name, 5, strlen($name)-5);
            $sql = "SELECT * FROM menuitem WHERE menuitem_id = '$id'";
            $result = mysqli_query($conn, $sql);
            while ($sqlResult = mysqli_fetch_assoc($result)) {
            $sub = $sqlResult['price_medium'] * $value;
                echo '
                        <td>' .  $sqlResult['productname'] . '</td>
                        <td>' .  $value . '</td>
                        <td>&euro; ' .  $sqlResult['price_medium'] . '</td>
                        <td>&euro; ' .  $sub . '</td>
                        <td><a href="includes/add-to-cart.inc.php?remove=' . $id . '">[-] </a><a href="includes/add-to-cart.inc.php?add=' . $id . '">[+] </a><a href="includes/add-to-cart.inc.php?delete=' . $id . '">[x]</a>
                      </tr>
                ';
            }
        }
        $total += $sub;
    }
}
echo $total;

}

EDIT for Chris:

I now have this:

function cart() {
include 'dbh.inc.php';
foreach($_SESSION as $name => $value) {
    if($value > 0) {
        if(substr($name, 0,5)=='cart_') {
            $id = substr($name, 5, strlen($name)-5);
            $sql = "SELECT * FROM menuitem WHERE menuitem_id = '$id'";
            $total = 0;
            $result = mysqli_query($conn, $sql);
            while ($sqlResult = mysqli_fetch_assoc($result)) {
            $sub = $sqlResult['price_medium'] * $value;
                echo '
                        <td>' .  $sqlResult['productname'] . '</td>
                        <td>' .  $value . '</td>
                        <td>&euro; ' .  $sqlResult['price_medium'] . '</td>
                        <td>&euro; ' .  $sub . '</td>
                        <td><a href="includes/add-to-cart.inc.php?remove=' . $id . '">[-] </a><a href="includes/add-to-cart.inc.php?add=' . $id . '">[+] </a><a href="includes/add-to-cart.inc.php?delete=' . $id . '">[x]</a>
                      </tr>
                ';
            }
        }
        $total += $sub; 
    }
}
echo $total;

}

enter image description here

itvba
  • 235
  • 9
  • 19
  • What throws the notice `$total += $sub;` or `echo $total;` if `+=` you need to initialize it. If the `echo` then you never enter the loop. – chris85 Nov 05 '17 at 15:03
  • Thank you for your answer! The $total += $sub throws the notice. Can you explain to me how i can do that? – itvba Nov 05 '17 at 15:08
  • Line one, or prior to the `+=` and not in the loops, `$total = 0;`. Assuming `$total` should be nothing to start with. – chris85 Nov 05 '17 at 15:13
  • Please look at my edit. I've added: $total = 0; Now i have 2 notice – itvba Nov 05 '17 at 15:18
  • `$total = 0;` needs to be **outside** the loops. You must also not be entering the SQL query, use some error reporting there, and/or verify you have a record that matches. – chris85 Nov 05 '17 at 15:20
  • But, $total = 0; *is* outside the loop right? The query works because the products are loaded with MySQLi – itvba Nov 05 '17 at 15:22
  • 1
    No, see `foreach($_SESSION as $name => $value) {` on first iteration you will not be set. Same with `$sub` at some point this running and neither value is set. You could just put a conditional around the addition so you aren't adding if it isn't set.. – chris85 Nov 05 '17 at 15:24
  • I'm stupid. It works now. Thanks a lot for your help :). Can you please put an answer in this topic so i can mark it as answered? – itvba Nov 05 '17 at 15:25
  • The question is a dup so I can't answer. – chris85 Nov 05 '17 at 15:27
  • Aww. Sorry bout that. Thanks anyway :) I really appreciate it. – itvba Nov 05 '17 at 15:28
  • @itvba Use this refined version of your code: http://sandbox.onlinephpfunctions.com/code/b39e915e4e77755177c9c4838f190bcd10265711 – mickmackusa Nov 06 '17 at 02:04

0 Answers0