kindly help me...... how to clear this error.. Error: Notice: Undefined variable: tot

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sindhukk
    New Member
    • Sep 2012
    • 1

    kindly help me...... how to clear this error.. Error: Notice: Undefined variable: tot

    hiii... i m trying to execute shopping cart program in php.
    this is my code, where i m getting the error.

    Code:
    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 book WHERE id='.$id);
                 while ($get_row= mysql_fetch_assoc($get)){
                   $sub = $get_row['Price']*$value;
                   echo $get_row['Name'].'x'.$value .'@'. $get_row['Price'].'='.$sub.'<a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.' ">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br/>';
    
    }
       }
       $total += $sub;
    
    }
    }
    Error message is:

    Error: Notice: Undefined variable: total in C:\wamp\www\try \cart.php on line 80
    Last edited by zmbd; Sep 25 '12, 08:25 PM. Reason: When posting code, please format it using the <CODE/> button.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    sindhukk,
    Please use the <CODE/> format button when posting your code. I've done this for you today.

    PHP is not one of my normal programing languages (actually, not at all)

    However, looking at your code reminds me some of C++ so I'll take a poke in the dark

    Line 7 opens the scope wherein $sub becomes a defined variable.

    Subsequently, $sub goes out of scope on line 11 when you close the braces.

    Thus, on line 13 when you attempt to set the $Total you are trying to use an undefined variable.

    I bow to the other experts/programmers if this is in error.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      However, looking at your code reminds me some of C++ so I'll take a poke in the dark

      Line 7 opens the scope wherein $sub becomes a defined variable.

      Subsequently, $sub goes out of scope on line 11 when you close the braces.
      I can say here that this part is not the case. PHP only has function scope, so $sub is available outside the while() loop. (otherwise there would have been a message about $sub not being defined).

      however, on line 13 you try to add to an uninitilised variable via +=, which causes the error. from the usage I assume that $total is a global variable already being set somewhere else. here strikes the function scope again, since you don’t have access to variables from an outer scope.
      what you would do here is returning the $sub value and add it in the original scope of $total.
      Code:
      function cart()
      {
          foreach ( ... )
          {
              ....
          }
          return $sub;
      }
      
      $total = 5;
      
      // ...
      
      $total += cart();

      Comment

      Working...