I'm looking for some help on calculating a total from lots of subtotals from a database, the code I'm using is working to calculate it, but the PHP echos an error saying that
Notice: Undefined variable: tot in ..............\viewing.php on line 192
But it is still calculating the total cost and echoing it, any ideas on how to get rid of that error?
I am getting the subtotals from a database using this:
while($row=mysql_fetch_array($result)) {
echo .....
$tot += $row['subtotal'];
}
At the bottom of the page, I've made it so it shows the total and its working, but its still giving me an error saying that the variable tot is undefined, any ideas?
It isn't an error, it's a notice!
Initialise
$tot = 0;
before your while loop
You need to define your $tot variable
Put this before using it in your loop
$tot = 0;
$tot = 0;
while($row=mysql_fetch_array($result)) {
echo .....
$tot += $row['subtotal'];
}
echo $tot;
From the php documentation:
It is not necessary to initialize variables in PHP however it is a
very good practice.
However it also says:
Relying on the default value of an uninitialized variable is
problematic in the case of including one file into another which uses
the same variable name. It is also a major security risk with
register_globals turned on. E_NOTICE level error is issued in case
of working with uninitialized variables..
You can turn off notices at a variable level by doing this:
while($row=mysql_fetch_array($result)) {
#$tot += $row['subtotal'];
}
Having said that best practice is:
$tot = 0;
while($row=mysql_fetch_array($result)) {
$tot += $row['subtotal'];
}
Related
I need a variable to be passed along several functions & if statements, i'm going to keep it short.
I start off with initializing a static counter which i will use to keep track of the case number in my mysql database;
static $counter = 1;
then i write my function in which i try to simply increment my global variable (this is in an if statement inside my function);
$counter++;
Now my code compiles and runs perfectly but the counter seems to never increment and give every case id 1.
Anyone know how i managed to mess this up?
EDIT (Current structure):
<?php
static $counter = 1;
function frontend($connection){
global $counter;
(...)
if(isset($_POST['submit'])){
(...)
if(isset($_POST['betaald'])){
$counter++;
}
}
} ...
Now this code makes a neat database of all i need except the counter which seems to be unchangeable.
Explain more about your code and see the example.
<?php
function keep_track() {
STATIC $count = 0;
$count++;
print $count;
print "<br />";
}
keep_track();
keep_track();
keep_track();
?>
This will produce the following result −
1
2
3
I have been trying to create a very simple shopping cart and I'm having a problem with my $_SESSION array. This is for a school project and I'm trying to make it as simple as possible.
The error I'm getting is:
Notice: Undefined index: cart in C:\xampp\htdocs\Final\menu.php on
line 31
Notice: Undefined offset: 5 in C:\xampp\htdocs\Final\menu.php on
line 31
if(isset($_GET['id'])){
$product_id = $_GET['id'];
$_SESSION['cart'][$product_id]++;
print_r($_SESSION);
print "<br>";
print_r($_GET);
}
Once I've added more than one item to a particular product_id, the error goes away. This is the way the tutorial I read explained to add items to the cart. Any suggestions?
Looks like $_SESSION['cart'] does not yet exist. Since it's going to be an array, instantiate it first with:
if(!array_key_exists('cart', $_SESSION)) $_SESSION['cart'] = array();
Since you have not yet assigned anything to $_SESSION['cart'][$product_id], you'll get this type of error when trying to increment it. You may want to try:
$_SESSION['cart'][$product_id] = (array_key_exists($product_id, $_SESSION['cart'])) ? $_SESSION['cart'][$product_id] +1 : 1;
or as an if statement:
if(array_key_exists($product_id, $_SESSION['cart'])) $_SESSION['cart'][$product_id]++;
else $_SESSION['cart'][$product_id] = 1;
When you do $_SESSION['cart'][$product_id]++; you are actually doing:
$_SESSION['cart'][$product_id] = $_SESSION['cart'][$product_id] + 1;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ generates warning if they keys do not exist yet
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ assigns without problems if they keys do not exist yet
The assignment with newly created keys is not the problem, the warning is generated by php trying to get the actual value of $_SESSION['cart'][$product_id].
To solve this you should properly initialize the variable:
$_SESSION['cart'][$product_id] = isset($_SESSION['cart'][$product_id])
? $_SESSION['cart'][$product_id]++
: 1;
Every time I try to run my code I am getting a 'Notice: Undefined offset: 1 Error
specifically coming from this line:
$acmark= $summary[1][1] += $student[$row][2] / 25;
I am new to PHP and would like to get this working with the least amount of changes possible. I have uploaded the full source code to Pastebin to make for easier viewing.
http://pastebin.com/Ur8u673V
Thanks in advance guys, Luke.
Looking at the code in your paste, you're initialising $summary to an empty array, and never actually adding anything to it before attempting to read the data. It's equivalent to:
$summary = array();
// ...
$acmark = $summary[1][1] += $student[$row][2] / 25; // $summary[1] isn't defined
Agree with George - you're initializing $summary as an array, but the offset starts at 0. Since you're incrementing in a loop, change line 140 to:
$acmark = $summary[][] += $student[$row][2] / 25;
That will eliminate the notice. You can apply the same solution to the other lines with undefined offsets as well to resolve the other notices.
Consider using an associative array(s) with foreach loops so the array keys are more meaningful. Associative arrays are easier to use and support the idea of using self-documenting code
i think u should set value of array first (maybe with 0), because if undefines is meaning that array is null value (null is different with 0)
add this code after $summary = array(); like below
$summary=array();
//additional code
for($sum=0; $sum<6; $sum++)
$summary[$sum][1]=0;
for($row=0; $row<25; $row++){
.
.
.
If I have an array:
$resultArr = pg_fetch_array($result,NULL);
and at the top of my php code I declare:
$_SESSION['resultArr'] = $resultArr;
Why can't I access the array elements like so:
for($i = 0; $i < $NUM_COLUMNS; $i++){
// creation of the table and row are handled elsewhere.
// The table is also within a <form> if that matters
echo "<td>" .$_SESSION['resultArr'][$i]."</td>";
}
My table ends up having empty columns and I can't figure out why...
EDIT: I figured it out. I was declaring $_SESSION['resultArr'] = $resultArr; at the top of my code (right after session_start()) and it wasn't getting set. I moved it down to the point right after $resultArr = pg_fetch_array($result,NULL);
Is this how it's supposed to work or should it have worked fine at the top of the code?
Maybe you did and didn't mention, but you must call session_start() before any operation on $_SESSION
after your edit, yes this is how it is supposed to work, you first need to declare $resultArr, then put it's value in the session array
beacause in php you are no longer working with pointers,
$_SESSION['resultArr'] = $resultArr; mean "$_SESSION['resultArr'] takes all the values of $resultArr at that precise moment", but it does not mean "they are thesame, if one changes, then the other changes too" .
<?php
include 'db.php';
$i=0;
$result15=mysql_query("select c.dishes from c");
while($row=mysql_fetch_array($result15))
{
if($row['dishes']!=NULL)
{
$dish[$i]=$row['dishes'];
$i++;
}
}
mysql_close();
$j=0;
while($j<=$i)
{
echo $dish[$j];
$j++;
}
?>
Getting notice: Undefined offset: 2 in F:\xampp\htdocs....on line 18
You mean while($j<$i) there.
Remember, you incremented $i after the last insert. This means that $i will be higher than the maximum key of $dish.
Some thoughts:
Any time you're testing for equality with null, you should consider using is_null (or !is_null). It is more accurate.
This:
$dish[$i]=$row['dishes'];
$i++;
Would be better as:
// obviously instead of $i you would use count($dish) later (or use foreach)
$dish[]=$row['dishes'];
That final while loop would be better as a foreach:
foreach($dish as $val)
{
echo $val;
}
You need to make sure the index exists before echoing it, as so:
while($j<=$i)
{
if (isset($dish[$j])) echo $dish[$j];
$j++;
}
Your second while loop is going to far. You only define dishes up to i-1 and then you loop through it up to and including i. Change it to:
while($j<$i)