I'm working on a form that writes on to a flat text file.
The form has 3 fields where the user may enter data
I'm using a loop to collect the values entered and to
write them into a text file.
Then the form has to read the text file and sort the
data (by item or amount) depending on the user's choice.
The problem is on the way the data retrieved displays:
I enter the following values:
+-----------------------------+
Item - Amount
+------------------------------+
Home - 100
Gas - 300
The result:
+-----------------------------+
Item - Amount
+------------------------------+
Home - 100
Home - 100
As you can see the values are being repeated
and the value containing "Gas" and "300" are
being ignored.
Question:
can someone recommedn a way to fix this?
Thank you in advance.
Dmonk
--------Entire Code follows: -----------
<html>
<head>
<title>Tester</title>
</head>
<body style="font-family: Arial, Helvetica, sans-serif; color: blue;">
<h1>One Column Example</h1>
<form method=post>
<b>Sort Order: </b>
<?php
$order = $HTTP_POST_VARS['order'];
switch($order)
{
case 'desc' :
print "Ascending <input type=radio name=order value=asc> ";
print "Descending <input type=radio name=order value=desc checked> ";
break;
default :
print "Ascending <input type=radio name=order value=asc checked> ";
print "Descending <input type=radio name=order value=desc> ";
break;
}
?>
<br><br>
<table>
<tr>
<th>Item</th>
<th>period</th>
<th>amount</th>
</tr>
<?php
//*************************************************
//Write out records with data if they exist.
//*************************************************
$action = $HTTP_POST_VARS['action'];
if ($action == 'update')
{
@ $fp = fopen("test.txt", 'w'); //Open for Write
$write_ctr = 1;
while (true)
{
$item_name = 'source'."$write_ctr";
$item_value = $HTTP_POST_VARS[$item_name];
$period_name = 'period'."$write_ctr";
$period_value = $HTTP_POST_VARS[$period_name];
$amount_name = 'amount'."$write_ctr";
$amount_value = $HTTP_POST_VARS[$amount_name];
if (empty($item_value)) {
break;
}
if (empty($amount_value)) {
break;
}
// Write out value to file
$outline = "$item_value*$period_value*$amount_value*\n";
fwrite($fp, $outline);
$write_ctr++;
}
fclose($fp);
}
//*************************************************
//Now Read Bills and Load and Sort Array
//*************************************************
$err_cnt = 0;
$read_ctr = 1;
$lines = array();
@ $fp = fopen("test.txt", 'r'); //Open file for reading
if ($fp)
{
while (true)
{
$line = fgets($fp,200);
if (feof($fp))
{
break;
}
$order = $sorter;
$fields = explode('*', $line);
$item_read = $fields[0];
$period_read = $fields[1];
$amount_read = $fields[2];
$new_line = "$item_value*$period_value*$amount_value*\n";
//add sorter if the user wants to sort by amount
array_push($lines, $new_line);
$read_ctr++;
}
if ($order == 'desc')
{
rsort($lines); //sort in reverse order
}
else {
sort($lines);
}
}
//*************************************************
//Now Read Sorted Array and Display
//*************************************************
$err_cnt = 0;
$array_ctr = 1;
foreach ($lines as $one_line)
{
$new_line = explode("*", $one_line);
$item_read = $fields[0];
$period_read = $fields[1];
$amount_read = $fields[2];
$item_name = 'source'."$array_ctr";
$period_name = 'period'."$array_ctr";
$amount_name = 'amount'."$array_ctr";
print '<tr>';
print "<td><input type=text name=$item_name value=$item_read></td>\n";
print "<td><input type=text name=$period_name value=$period_read></td>\n";
print "<td><input type=text name=$amount_name value=$amount_read></td>\n";
print "<td>";
print '</tr>';
$array_ctr++;
}
//*************************************************
//Now write the blank lines
//*************************************************
for ($i = $array_ctr; $i < $array_ctr + 2; $i++)
{
$item_name = 'source'."$i";
$period_name = 'period'."$i";
$amount_name = 'amount'."$i";
print '<tr>';
print "<td><input type=text name=$item_name value=''></td>\n";
print "<td><input type=text name=$period_name value=''></td>\n";
print "<td><input type=text name=$amount_name value=''></td>\n";
print '</tr>';
}
?>
</table>
<br><input type=submit value=Submit>
<br<br>
<!-- Hidden Action Field -->
<input type=hidden name=action value=update>
</form>
</body>
</html>