School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 358,764 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,782 people online right now.Registration is fast and FREE... Join Now!



Totalling Items in a loop

52 Weeks of Code Challenge: Android

Week #11 of the 52 Weeks of Code Challenge is Android, you should give it a shot. Click Here!

Totalling Items in a loop Rate Topic: -----

#1 sententia  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-July 09


Dream Kudos: 0

Posted 11 August 2009 - 04:20 PM

I've created a page that I need a bit of help with...

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

Was This Post Helpful? 0
  • +
  • -


#2 JackOfAllTrades  Icon User is offline

  • Mayor of Simpleton
  • Icon

Reputation: 760
  • View blog
  • Posts: 8,112
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Re: Totalling Items in a loop

Posted 11 August 2009 - 04:35 PM

Here:
$total = 0;
$total = $total + $amount_value_readfromfile;


You're setting $total here, but you're using $amount_value_readfromfile...and you haven't read anything into that value yet, and you never modify $total again!
Was This Post Helpful? 1
  • +
  • -

#3 sententia  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-July 09


Dream Kudos: 0

Re: Totalling Items in a loop

Posted 11 August 2009 - 06:20 PM

View PostJackOfAllTrades, on 11 Aug, 2009 - 04:35 PM, said:

Here:
$total = 0;
$total = $total + $amount_value_readfromfile;


You're setting $total here, but you're using $amount_value_readfromfile...and you haven't read anything into that value yet, and you never modify $total again!


Im a bit confused as to where to go next... works when the values is not a number... it throws an error here

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++;
				}


Which makes me think it is reading that value. Would it help to get rid of
$total = 0;
above
$total = $total + $amount_value_readfromfile;
	$amount_value_readfromfile = trim($amount_value_readfromfile);


I was trying to use
$amount_value_readfromfile++;
inside of
function read_and_display_table()
. Couldnt get it to work initially, might that be the problem.

I am going through the code and trying to logically figure out what exactly I did, any more help would be greatly appreciated. Thanks.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month