473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. function cart(){
  2.    foreach($_SESSION as $Name => $value) {
  3.      if($value > 0) {
  4.         if(substr($Name, 0, 5)=='cart_') {
  5.             $id = substr($Name, 5, (strlen($Name)-5));
  6.             $get = mysql_query('SELECT id, Name, Price FROM book WHERE id='.$id);
  7.              while ($get_row= mysql_fetch_assoc($get)){
  8.                $sub = $get_row['Price']*$value;
  9.                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/>';
  10.  
  11. }
  12.    }
  13.    $total += $sub;
  14.  
  15. }
  16. }
Error message is:

Error: Notice: Undefined variable: total in C:\wamp\www\try\cart.php on line 80
Sep 25 '12 #1
2 2158
zmbd
5,501 Expert Mod 4TB
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.
Sep 25 '12 #2
Dormilich
8,658 Expert Mod 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. function cart()
  2. {
  3.     foreach ( ... )
  4.     {
  5.         ....
  6.     }
  7.     return $sub;
  8. }
  9.  
  10. $total = 5;
  11.  
  12. // ...
  13.  
  14. $total += cart();
Sep 26 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hal Halloway | last post by:
How do I fix this error message below? Thanks. Notice: Undefined index: key_word in C:\Program Files\Apache Group\Apache2\htdocs\search010.php on line 55 .... if( $_GET ) { // line 55 //...
3
by: Mike | last post by:
I'm new to PHP - moving over from ASP. I have a number of include files, the first of which sets the value of a variable $loginmsg. I use that variable in a subsequent include file, but get a...
1
by: prabhunew2005 | last post by:
hi I am doing web portal design using php. I gave part of one of my screen coding. <html> <head> <script language = "javascript"> function list_all_click()
4
by: dmain1971 | last post by:
I have a class with an empty array like so: private $detail = array(); Then I have a function later on in the class: function get_array_value() { $detail_info = ' ';
3
by: printline | last post by:
Can anyone help me with why i am getting this error message: Notice: Undefined variable: SalesRepID in C:\Inetpub\wwwroot\index.php on line 158 Here is my php code: <?php session_start();...
2
by: norwichchris | last post by:
hi, I am having serious trouble with my PHP Postcard script. The error message i get is: Notice: Undefined index: Notice: Undefined variable Basically it does not send out the scripts at...
1
by: bob johnson | last post by:
Notice: Undefined variable: db_host in C:\wamp\www\cbmall\index.php on line 7 Notice: Undefined variable: db_user in C:\wamp\www\cbmall\index.php on line 7 Notice: Undefined variable: db_pass...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.