Forum Moderators: coopster

Message Too Old, No Replies

undefined variables - totals

         

ckdoublenecks

5:05 pm on Aug 13, 2011 (gmt 0)

10+ Year Member



the below code works fine but i get the below messages. what must i do to initialize the variables and eliminate the messages?
<?php
mysql_connect("localhost","root","");
mysql_select_db('oodb') or die( "Unable to select database");
$query=" SELECT * FROM oocust WHERE payrec = 'P'AND pd = ' '";
$result=mysql_query($query);
$num=mysql_numrows($result);
echo date("m/d/Y") . "<br />";
echo "<font size=+2><b> Old Orchard Plumbing A/P Due Report</font></b></b><br />";
echo "<table cellspacing=1 cellpadding=0 border=0>
<tr>
<th colspan=2></th>
<th align=left>order</th>
<th>days</th>
<th>ship</th>
<th></th>
<tr>
<th>Inv#</th>
<th>Customer</th>
<th align=left>date</th>
<th>late</th>
<th>amt</th>
<th align=right>Charges</th>
<th align=right>tax</th>
<th align=right>Owed</th>
<tr>
<TH colspan=9>=======================================================================</TH>
</tr>";
while($row = mysql_fetch_array($result))
{
$totcharges = $totcharges + $row['charges'];
$tottax = $tottax + $row['tax'];
$totamtdue = $totamtdue + $row['amtdue'];
$totship = $totship + $row['shipamt'];
echo "<tr>";
echo "<td>" . $row['invnum'] . "</td>";
echo "<td>" . $row['bname'] . "</td>";
echo "<td>" . $row['dateord'] . "</td>";
echo "<td align=right>" . $row['dayslate'] . "</td>";
echo "<td align=right>" . $row['shipamt'] . "</td>";
echo "<td align=right>" . $row['charges'] . "</td>";
echo "<td align=right>" . $row['tax'] . "</td>";
echo "<td align=right>" . $row['amtdue'] . "</td>";
}
echo "<tr>";
echo "<th colspan=9>=======================================================================</TH>";
echo "<tr>";
echo "<td>Gtotals</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td align=right>" . sprintf("%.2f",$totship) .
"</td>";
echo "<td align=right>" . sprintf("%.2f",$totcharges) .
"</td>";
echo "<td align=right>" . sprintf("%.2f",$tottax) .
"</td>";
echo "<td align=right>" . sprintf("%.2f",$totamtdue) .
"</td>";
echo "</tr>";
echo "</table>";
mysql_close();
?>

Notice: Undefined index: totcharges in C:\xampp\htdocs\invoice\apdue.php on line 7

Notice: Undefined index: tottax in C:\xampp\htdocs\invoice\apdue.php on line 8

Notice: Undefined index: totamtdue in C:\xampp\htdocs\invoice\apdue.php on line 9

Notice: Undefined index: totship in C:\xampp\htdocs\invoice\apdue.php on line 10
08/13/2011
Old Orchard Plumbing A/P Due Report
order days ship
Inv# Customer date late amt Charges tax Owed
=======================================================================
154 kennys machine shop 10/15/2010 270 0.00 5.35 0.31 5.66
=======================================================================
Gtotals

penders

5:32 pm on Aug 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm a little puzzled as to why you are getting "Notice: Undefined index" when the code you have posted should be outputting "Notice: Undefined variable"? (Also, the line numbers of the notices do not appear to match up with your code?!)

Anyway, you should initialise these variables to 0 before you add something to them in your loop, particularly since you are using variables in the global scope (if another script used the same global variable, your script would simply output the wrong result with no warning). PHP is implicitly initialising these variables for you but then issuing a notice.

$totcharges = 0; 
$tottax = 0;
$totamtdue = 0;
$totship = 0;

while($row = mysql_fetch_array($result))
{
$totcharges = $totcharges + $row['charges'];
$tottax = $tottax + $row['tax'];
$totamtdue = $totamtdue + $row['amtdue'];
$totship = $totship + $row['shipamt'];


You would normally get "Notice: Undefined index" if you tried to access an undefined array index by that name in an associative array. eg: $row['totcharges']

rocknbil

4:22 pm on Aug 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The index **should** be referring to the $row variable, not the $scalar variables, otherwise it would be undefined variable. What this **usually** means is that your database field names are different than what you're referring to - case structure maybe? But as mentioned, those $row indices aren't referenced.

ID|Charges|Tax|Amtdue|Shipamt....

$row['Charges'];
$tottax = $tottax + $row['Tax'];
$totamtdue = $totamtdue + $row['Amtdue'];

... etc., or similar.

@penders: it might be a better idea to put those inside the while/if, so it doesn't maintain the value of the last iteration:


while($row = mysql_fetch_array($result))
{
$totcharges=$tottax=$totamtdue=$totship=0;

penders

6:08 pm on Aug 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@penders: it might be a better idea to put those inside the while/if, so it doesn't maintain the value of the last iteration:


But I think that's the whole point; to sum all the values and get the total? If you initialise the variables on every iteration of the loop (ie. inside the loop) then you're not going to get the totals.

rocknbil

3:24 pm on Aug 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was under the impression this was outputting rows of accounts so you want the totals for each account. If not . . . <facepalm> :-)

ckdoublenecks

5:21 pm on Aug 16, 2011 (gmt 0)

10+ Year Member



thanks, guys. that did it