Concatinating a string with a simple counter?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BryanD
Forum Newbie
Posts: 5
Joined: Sat Jul 16, 2005 8:29 pm

Concatinating a string with a simple counter?

Post by BryanD »

This may seem simple but I've only been working with PHP for about 3 weeks. I'm trying to clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>
<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end form -->
</form>
</body>
</html>ith PHP for about 3 weeks. I'm trying to clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>
<p>
<!-- html submit button clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

&lt;?php

print &quote;&lt;table&gt;&quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;/td&gt;&lt;/tr&gt;&quote;;
}

// end table
print &quote;&lt;/table&gt;&quote;;

// print error or total expense
if ($error &gt; 0)
{
print &quote;&lt;font color=#FF0000&gt;Error: $error&lt;/font&gt;&quote;;
}else{
$total_expense = &quote;$amount1&quote; + &quote;$amount2&quote;+ &quote;$amount3&quote; + &quote;$amount4&quote;;
if ($total_expense == 0)
{
print &quote;Total Expenses:&quote;;
}else{
print &quote;Total Expenses: $total_expense&quoteexpenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

&lt;?php

print &quote;&lt;table&gt;&quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;/td&gt;&lt;/tr&gt;&quote;;
}

// end table
print &quote;&lt;/table&gt;&quote;;

// print error or total expense
if ($error &gt; 0)
{
print &quote;&lt;font color=#FF0000&gt;Error: $error&lt;/font&gt;&quote;;
}else{
$total_expense = &quote;$amount1&quote; + &quote;$amount2&quote;+ &quote;$amount3&quote; + &quote;$amount4&quote;;
if ($total_expense == 0)
{
print &quote;Total Expenses:&quote;;
}else{
print &quote;Total Expenses: $total_expense&quote;;
}
}

?&gt;

[/php:1:01a0b8lowing code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

&lt;?php

print &quote;&lt;table&gt;&quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;/td&gt;&lt;/tr&gt;&quote;;
}

// end table
print &quote;&lt;/table&gt;&quote;;

// print error or total expense
if ($error &gt; 0)
{
print &quote;&lt;font color=#le but I've only been working with PHP for about 3 weeks. I'm trying to clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>
<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end form -->
&ltle but I've only been working with PHP for about 3 weeks. I'm trying to clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;amp;gt;
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>
<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end form -->
</form>
</body>
</html>mp;quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in ao clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print &quote;&lt;table&gt;&quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;/td&gt;&lt;/tr&gt;&quote;;
}

// end table
print &quote;&lt;/table&gt;&quote;;

// print error or total expense
if ($error &gt; 0)
{
print &quote in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

&lt;?php

print &quote;&lt;table&gt;&quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;/td&gt;&lt;/tr&gt;&quote;;
}

// end table
print &quote;&lt;/table&gt;&quote;;

// print error or total expense
if ($error &gt; 0)
{
print &quote;&lt;font color=#FF0000&gt;Error: $error&lt;/font&gt;&quote;;
}else{
$total_expense = &quote;$amount1&quote; + &quote;$amount2&quote;+ &quote;$amount3&quote; + &quote;$amount4&quote;;
if ($for about 3 weeks. I'm trying to clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quotchanging the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

&lt;?php

print &quote;&lt;table&gt;&quote;;

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&lt;/td&gt;&lt;/tr&gt;&quote;;
}

// end table
print &quote;&lt;/table&gt;&quote;;

// print error or total expense
if ($error &gt; 0)
{
print &quote;&lt;font color=#FF0000&gt;Error: $error&lt;/font&gt;&quote;;
}else{
$total_expense = &quote;$amount1&quote; + &quote;$amount2&quote;+ &quote;$amount3&quote; + &quote;$amount4&quote;;
if ($total_expense == 0)
{
print &quote;Total Expenses:&quote;;
}else{
print &quote;Total Expenses: $total_expense&quote;;
}
}

?&gt;
<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end formit?
Thanks for any and all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i &lt;3; $i++)
{
$item_name = 'item'.&quote;$i&quote;; // item using $i as the loop counter
$item_value = $_POST&#1111;$item_name];
$amount_name = 'amount'.&quote;$i&quote;; // amount using $i as the loop counter
$amount_value = $_POST&#1111;$amount_name];
$print_error = &quote;&quote;; // set print error to null
$bills = &quote;bills.txt&quote;;
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '&lt;tr&gt;';

// item field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$item_name value='$item_value'&gt;&quote;;
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input type=text name=$amount_name value='$amount_value'&gt;&quote;;

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = &quote;Amount $amount_value is not a number&quote;;
$error++;
}else{

}
print &quote;&lt;font color=#FF0000&gt;$print_error&lt;/font&gt;&quote;;
}
// end each table row
print &quote;&ampnly been working with PHP for about 3 weeks. I'm trying to clean up the addition in the following code ($total_expenses) to make room if I have to add more cells to the table by changing the $i variable. Also if I wanted to add 2 new blank rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print &quote;&amp;nbsp;&lt;/td&gt;&quote;;

// amount field setup
print &quote;&lt;td&gt;&quote;;
print &quote;&lt;input t rows after submitting the form for more possible entries where/how would be a good way of going about it?
Thanks for any and all help.
 
<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>
<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end form -->
</form>
</body>all help.

<p></p>
<form method="post">

<table width="316">
<th width="142"><font color="#0033FF">Item</font></th>
<th width="162"><font color="#0033FF">Amount</font></th>

</table>

Code: Select all

<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>
<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end form -->
</form>
</body>
</html>
<?php

print "<table>";

//set server to display errors.
ini_set('display_errors',1);

for ($i = 1; $i <3; $i++)
{
$item_name = 'item'."$i"; // item using $i as the loop counter
$item_value = $_POST[$item_name];
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
$amount_value = $_POST[$amount_name];
$print_error = ""; // set print error to null
$bills = "bills.txt";
$fp = fopen ($bills, 'w');

// Set up for table rows and columns
print '<tr>';

// item field setup
print "<td>";
print "<input type=text name=$item_name value='$item_value'>";
print "&nbsp;</td>";

// amount field setup
print "<td>";
print "<input type=text name=$amount_name value='$amount_value'>";

// Handle Null field
if (empty ($amount_value))
{
}else{
// Check for number in amount field
if (!is_numeric($amount_value))
{
$print_error = "Amount $amount_value is not a number";
$error++;
}else{

}
print "<font color=#FF0000>$print_error</font>";
}
// end each table row
print "</td></tr>";
}

// end table
print "</table>";

// print error or total expense
if ($error > 0)
{
print "<font color=#FF0000>Error: $error</font>";
}else{
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
if ($total_expense == 0)
{
print "Total Expenses:";
}else{
print "Total Expenses: $total_expense";
}
}

?>



<p>
<!-- html submit button -->
<input type="submit" name="Submit" value="Save">
</p>
<!-- end form -->
</form>
</body>
</html>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I don't understand your question at all. But looking at your code it looks like you're trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?king at your code it looks like you're trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're rying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
e var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't
you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in your question at all. But looking at your code it looks like you're trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying t your question at all. But looking at your code it looks like you're trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?nd what you're trying to accomplish, can you try to explain in a little more depth?trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?ogether.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugyour code it looks like you're trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?ying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand " + "$amount4";

that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?n at all. But looking at your code it looks like you're trying to add strings together.

you need to keep the var types in mind: (you can't add strings)

I assume you are trying to add numbers?

this line here:

Code: Select all

$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";
that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?
$total_expense = "$amount1" + "$amount2"+ "$amount3" + "$amount4";

that won't work...or if it does, it's horribly ugly 8O

if you're trying to create a set of numbers and then add them together, lose the quotes.

again I really couldnt understand what you're trying to accomplish, can you try to explain in a little more depth?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I think I know what you're asking, but like burrito says, I'm not sure you really want to do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to build up an array of amounts and then loop over that to sum?to says, I'm not sure you really want to do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i&lt;5;$i++)
{
    $newVar=&quote;$baseVar&quote;.&quote;$i&quote;;
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
 do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr=&quote;amount&quote;;
$totalAmount=0;
for($i=1;$i&lt;5;$i++)
{
    $newVar=&quote;$baseVar&quote;.&quote;$i&quote;;
    $totalAmount+=$$newVar; // note the double dollarsign
}
echos known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$ut how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to build up an array of amounts and then loop over that to sum?t you're asking, but like burrito says, I'm not sure you really want to do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to t you're asking, but like burrito says, I'm not sure you really want to do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to build up an array of amounts and then loop over that to sum?en loop over that to sum?o do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to build up an array of amounts and then loop over that to sum? asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find var I'm not sure you really want to do that.

If you're asking about how to make what's known as "variable variables" you could do:

Code: Select all

$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
    $newVar="$baseVar"."$i";
    $totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;
Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to build up an array of amounts and then loop over that to sum?
$baseVarr="amount";
$totalAmount=0;
for($i=1;$i<5;$i++)
{
$newVar="$baseVar"."$i";
$totalAmount+=$$newVar; // note the double dollarsign
}
echo $totalVar;


Now I find variable variables very useful, but this doesn't look like one of those times.

Perhaps you want to build up an array of amounts and then loop over that to sum?
BryanD
Forum Newbie
Posts: 5
Joined: Sat Jul 16, 2005 8:29 pm

Post by BryanD »

Buritto thanks I knew it was ugly but it did work.

Now let's see if I can clarify what I'm tring to do.
When the page initially comes up in a browser it looks like this:

Image

After the user has entered the amounts in that field and clicked save I would like the page to reappear with both what they input, the total or error and two new blank rows to add more things if they need. I was hoping to use the for loop counter to add the additional rows. so it would look like the following:

Image

nielsene,
I'm not sure I get using a variable variable to do this.
Would that replace this?

Code: Select all

for ($i = 1; $i <3; $i++)  
		{
$amount_name = 'amount'."$i"; // amount using $i as the loop counter
			$amount_value = $_POST[$amount_name];
From my original code?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Yeah, then nevermind me :)

So it sounds like you want to set up two loops:
The first loops through stored/newly entered values to redisplay previsouly entered
The second loops through adding the blank rows.

Thus you'll always have a fixed number of blanks, no matter how many you enter.
BryanD
Forum Newbie
Posts: 5
Joined: Sat Jul 16, 2005 8:29 pm

Post by BryanD »

Exactly.
Now please correct me if I'm wrong but shouldn't the add new rows loop go within the stored/empty loop (a nested for loop)?

I think I might be on the right track now :)
Thanks
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I wouldn't nest them.

In psuedo-code

Code: Select all

// setup form <form action...> any hidden...
for ($i=0;$i<numStored;$i++) {
  // output a table row
  // increment your total counter if needed
}
for ($i=numStored;$i<$numStored+$numBlanks;$i++) {
  // output a blank table row
}
// add submit, close form
Since you want a fixed number of blank rows. If you nested them you'd end up with $numBlank*$numStored blank rows.
BryanD
Forum Newbie
Posts: 5
Joined: Sat Jul 16, 2005 8:29 pm

Post by BryanD »

nielsene,
Thanks much. That did the trick. :D

Could you help me with on more line?

Code: Select all

$total_expense = $amount0 + $amount1 + $amount2 + $amount3 + $amount4;
The above is derived by concatinating a variable to a counter. Is there a way to make this keep adding the $amountx variable indefinately?
ie: if $amount (0-4)is filled with data then more is added $amount5.
you would have this line change to:

Code: Select all

$total_expense = $amount0 + $amount1 + $amount2 + $amount3 + $amount4 + $amount5;
Without having to edit the code by hand.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Either stick the variables into an array, or use the variable variables trick I showed before.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

First dynamically make the number of fields you need.You could store the total number of fields you create in a hidden field. Then when the person hits submit you just add two more. Echo out the new total in the hidden field. Name all the amount fields amount+sum_num. Use a loop to echo them out. To get the amounts out you can create a loop. Inside the loop you add the amounts something like this:

Code: Select all

while ($x = 0; $x <= $total_num_of_fields; $x++){
   $cost[] =  $_POST['amount'.$x];
   
}
   $total = array_sum($cost);
Hope that gets you started.
BryanD
Forum Newbie
Posts: 5
Joined: Sat Jul 16, 2005 8:29 pm

Post by BryanD »

Eric Many thanks. the variable variable trick worked like a champ. :D
Post Reply