Page 1 of 1

Why do i get "Notice: Undefined variable" message

Posted: Tue Jan 15, 2008 1:44 pm
by drors30
Hi,
Please look at the attached code down below.
In lines 70,71 and 72 there are 3 variables defined namely :
$balancetotals
$paymenttotals
$taxcollected

However while trying to echo the results in lines 82,86,90
an error message appear about "Notice: Undefined variable"

*** PLEASE USE

Code: Select all

TAG ***[/color]

Code: Select all

<?php
 
// Include session check and database connection:
include("../config.php"); include("admin_session_check.php");
 
// Assign values into the invoices database table:
$getinvoices = mysql_query("SELECT * FROM invoices WHERE status = 'Awaiting payment.' OR status = 'Partial payment received.' ORDER BY invoiceid DESC", $sqlconnection)
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Administrator Overview</title>
<link href="../icdbill.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<fieldset>
<legend>Administrator Overview:</legend>
<table width="100%">
  <tr>
    <td><?php include("admin_navbar.php") ?></td>
  </tr>
</table>
<hr >
<table width="100%" id="list">
  <tr>
    <td class="tabletop">Calories:</td>
    <td class="tabletop">Client:</td>
    <td class="tabletop">Invoice Date:</td>
    <td class="tabletop">Due Date</td>
    <td class="tabletop">Status:</td>
    <td class="tabletop">Balance:</td>
    <td width="14%" class="tabletop">Options:</td>
  </tr><?php
 
// Show the invoice list:
while($invoices = mysql_fetch_array($getinvoices)) {
 
// Convert clientid into a name for display purposes:
$clientid = $invoices['clientid'];
$getclients = mysql_query("SELECT * FROM clients WHERE clientid = '$clientid'", $sqlconnection);
$clients = mysql_fetch_array($getclients)
 
?>
  <tr>
    <td><?php echo $invoices['invoiceid'] ?></td>
    <td>[<A href="modify_client.php?clientid=<?php echo $clients['clientid'] ?>"><?php echo $clients['name'] ?></a>]</td>
    <td><?php echo $invoices['date'] ?></td>
    <td><?php echo $invoices['duedate'] ?></td>
    <td><?php echo $invoices['status'] ?></td><?php
 
// Get balance calculated:
$subtotal = $invoices['subtotal'];
$discount = $subtotal * ($invoices['discount'] / 100);
$discountedsubtotal = ($subtotal - $discount);
$tax = $discountedsubtotal * ($invoices['tax'] / 100);
$amountdue = ($tax + $discountedsubtotal);
$received = $invoices['received'];
$balance = ($amountdue - $received)
 
?>
    <td><?php echo number_format($balance, 2) ?></td>
    <td><A href="file://C:\wamp\www\billing\invoice\print_invoice.php?invoiceid=<?php echo $invoices['invoiceid'] ?>"><IMG height=16 alt="Print Invoice" src="file://C:\wamp\www\billing\images\icons\print_page.png" width=16></a> · 
      <A href="modify_invoice.php?invoiceid=  <?php echo $invoices['invoiceid'] ?>"><IMG height=16 alt="Modify Invoice" src="file://C:\wamp\www\billing\images\icons\modify_invoice.png" width=16></a>.<A href="new_payment.php?invoiceid=  <?php echo $invoices['invoiceid'] ?>"><IMG height=16 alt="New Payment" src="../images/icons/new_payment.png" width=16 ></a> 
      · <A href="delete_invoice_code.php?invoiceid=  <?php echo $invoices['invoiceid'] ?>"><IMG height=16 alt="Delete Invoice" src="../images/icons/delete.png" width=16 ></a></td>
  </tr><?php
 
$balancetotals = $balancetotals + $balance;
$paymenttotals = $paymenttotals + $received;
$taxcollected = $paymenttotals * ($invoices['tax'] / 100);
 
// End invoice listing:
}
 
?>
</table>
<hr >
<table width="100%">
  <tr>
    <td width="20%">Total Balances Due:</td>
 <td><?php echo number_format($balancetotals,2)?></td>
  </tr>
  <tr>
    <td>Total Balances Paid:</td>
    <td><?php echo number_format($paymenttotals,2)?></td>
  </tr>
  <tr>
    <td>Total TAX Collected:</td>
    <td><?php echo number_format($taxcollected, 2)?></td>
  </tr>
</table>
</fieldset>
 
</body>
</html>

Re: Why do i get "Notice: Undefined variable" message

Posted: Tue Jan 15, 2008 3:56 pm
by SpecialK
To me it looks like you are initializing them in the WHILE loop. If initialized there I believe the scope of them remains there. Set them to 0 prior to the loop and you should be fine.

Re: Why do i get "Notice: Undefined variable" message

Posted: Tue Jan 15, 2008 5:02 pm
by tim2
If they were initialised then they will be in scope, but if the query returns no results the code within the while loop is never executed. Do what SpecialK suggested and set them to 0 before the while loop.

Re: Why do i get "Notice: Undefined variable" message

Posted: Tue Jan 15, 2008 9:53 pm
by jimthunderbird
If you are sure some variables are going to be used later on, define them at the beginning first.

Re: Why do i get "Notice: Undefined variable" message

Posted: Tue Jan 15, 2008 10:17 pm
by Christopher
Just look at where those variables are first used:

Code: Select all

$balancetotals = $balancetotals + $balance;
$paymenttotals = $paymenttotals + $received;
$taxcollected = $paymenttotals * ($invoices['tax'] / 100);
I see where $balance is assigned a value above, but what is $balancetotals set to? It is undefined -- just as the error message says. You need to set those to zero at the top.