I have a form that is using a loop to run through some values, but I cannot seem to get all of these values to total up at the end.
<html>
<head>
<title>My Bills</title>
</head>
<body style="font-family: Arial, Helvetica, sans-serif; color:#333333;">
<h1>My Bills</h1>
<form method=post>
<table>
<tr>
<th>Item</th>
<th>Amount</th>
</tr>
<?php
//***********************
// Main Logic
//***********************
$action = $_POST['action'];
// This code only runs after the form is fully display and the user
// clicks the submit button. That is... it is not run the first time
// you open this page from a link.
if ($action == 'update')
{
write_to_file( );
}
// This function reads all the lines from the file
// and writes out the HTML table. It keeps track
// of the number of lines read and returns that
// value into $read_cntr_returned
$read_cntr_returned = read_and_display_table( );
// This function writes out the two blank lines
// using the integer in $read_cntr_returned as the
// starting row number.
write_blank_lines($read_cntr_returned);
print("</table>");
if ($err_cnt > 0) {
print "<br>Number of Errors: $err_cnt <br>";
}
else
{
print "<br>There are no errors <br>";
print "<br>Total Bills: $total <br>";
}
//***********************
?>
<?php
//*************************************************
//Write out records with data if they exist.
//*************************************************
function write_to_file( )
{
$fp = fopen("test.txt", "w");
$write_ctr = 1;
while (true)
{
$item_name = 'item'."$write_ctr";
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$write_ctr";
$amount_value = $_POST[$amount_name];
if (empty($item_value))
{
break;
}
// Write out value to file
$outline = "$write_ctr*$item_value*$amount_value*\n";
fwrite($fp, $outline);
$write_ctr++;
}
fclose($fp);
}
//*************************************************
//Now Read Bills and Display
//*************************************************
function read_and_display_table()
{
global $err_cnt;
$err_cnt = 0;
$read_ctr = 1;
$amount_value_readfromfile = 0;
@ $fp = fopen("test.txt", 'r'); //Open file for reading
$total = 0;
$total = $total + $amount_value_readfromfile;
$amount_value_readfromfile = trim($amount_value_readfromfile);
if ($fp)
{
while(true)
{
$line = fgets($fp);
if (feof($fp)) {
break;
}
list($counter, $item_value_readfromfile, $amount_value_readfromfile) = explode("*", $line);
$item_name = 'item'."$read_ctr";
$amount_name = 'amount'."$read_ctr";
print '<tr>';
print "<td><input type='text' name='$item_name' value='$item_value_readfromfile'></td>\n";
print "<td><input type='text' name='$amount_name' value='$amount_value_readfromfile'></td>\n";
print "<td>";
if (!is_numeric($amount_value_readfromfile))
{
print "<td><font color=red>I'm sorry, but the amount must be a number.</font></td>\n";
$err_cnt++;
}
print '</tr>';
$read_ctr++;
}
}
return $read_ctr;
}
//*************************************************
//Now write the blank lines
//*************************************************
function write_blank_lines($read_ctr_received)
{
for ($i = $read_ctr_received; $i < $read_ctr_received + 2; $i++)
{
$item_name = 'item'."$i";
$amount_name = 'amount'."$i";
print '<tr>';
print "<td><input type=text name=$item_name value=''></td>\n";
print "<td><input type=text name=$amount_name value=''></td>\n";
print '</tr>';
}
}
?>
<br><input type="submit" value="Submit">
<br><br>
<!-- Hidden Action Field -->
<input type="hidden" name="action" value="update">
</form>
</body>
</html>
This is the section of the above code that seems to be causing the issue:
function read_and_display_table()
{
global $err_cnt;
$err_cnt = 0;
$read_ctr = 1;
@ $fp = fopen("test.txt", 'r'); //Open file for reading
$total = 0;
$total = $total + $amount_value_readfromfile;
$amount_value_readfromfile = trim($amount_value_readfromfile);
if ($fp)
{
while(true)
{
$line = fgets($fp);
if (feof($fp)) {
break;
}
list($counter, $item_value_readfromfile, $amount_value_readfromfile) = explode("*", $line);
$item_name = 'item'."$read_ctr";
$amount_name = 'amount'."$read_ctr";
print '<tr>';
print "<td><input type='text' name='$item_name' value='$item_value_readfromfile'></td>\n";
print "<td><input type='text' name='$amount_name' value='$amount_value_readfromfile'></td>\n";
print "<td>";
if (!is_numeric($amount_value_readfromfile))
{
print "<td><font color=red>I'm sorry, but the amount must be a number.</font></td>\n";
$err_cnt++;
}
print '</tr>';
$amount_value_readfromfile++;
$read_ctr++;
}
}
return $read_ctr;
}
[/CODE}
It should be printing the value of $total towards the top here:
[CODE]
write_blank_lines($read_cntr_returned);
print("</table>");
if ($err_cnt > 0) {
print "<br>Number of Errors: $err_cnt <br>";
}
else
{
print "<br>There are no errors <br>";
print "<br>Total Bills: $total <br>";
}
I can get the errors to print... but for some reason the total of the bills will not. This is section that is printing the values:
write_blank_lines($read_cntr_returned);
print("</table>");
if ($err_cnt > 0) {
print "<br>Number of Errors: $err_cnt <br>";
}
else
{
print "<br>There are no errors <br>";
print "<br>Total Bills: $total <br>";
}
If someone could take a look at the code and let me know if they see anything that would be outstanding. Thanks A lot.
This post has been edited by sententia: 11 August 2009 - 04:23 PM

Start a new topic
Add Reply





MultiQuote

| 


