我有一个页面显示了一些可以通过以下代码保存到文件中的字段:
<?php
if($_POST['Submit'])
{
$company = $_POST['company'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$address4 = $_POST['address4'];
$contact = $_POST['contact'];
$contact2 = $_POST['contact2'];
$phone = $_POST['phone'];
$data = "$company \n $address1 \n $address2 \n $address3 \n $address4 \n $contact \n $contact2 \n $phone\n";
//open the file
$fh = fopen("/cfg/customers.txt", "a");
fwrite($fh, $data);
//close the file
fclose($fh);
print "Information Submitted. Thanks";
}
else
{
print <<<ENDOFTXT
</style><form name="form1" method="post" action="index.php">
<table width="780" border="0" align="center">
<tr>
<td width="256"><span class="style5">Company Name:</span></td>
<td width="514"><input name="company" type="text" id="company"></td>
</tr>
<tr>
<td><span class="style5">Address
:</span></td>
<td><input name="address1" type="text" id="address1" value=""></td>
</tr>
<tr>
<td><span class="style5">Unit / Apt :</span></td>
<td><input name="address2" type="text" id="address2"></td>
</tr>
<tr>
<td><span class="style5">City, Pr : </span></td>
<td><input name="address3" type="text" id="address3"></td>
</tr>
<tr>
<td><span class="style5">Postal Code :</span></td>
<td><input name="address4" type="text" id="address4"></td>
</tr>
<tr>
<td><span class="style5">Contact Name: </span></td>
<td><input name="contact" type="text" id="contact" value="First Name Last Name"></td>
</tr>
<tr>
<td><span class="style5">Email: </span></td>
<td><input name="contact2" type="text" id="contact2"></td>
</tr>
<tr>
<td><span class="style5">Phone :</span></td>
<td><input name="phone" type="text" id="phone" value="( ) ">
<input name="Submit" type="submit" class="style5" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><div align="center"></div></td>
</tr>
</table>
<p align="center"> </p>
</form>
ENDOFTXT;
}
?>
现在,我可以使用以下代码读取文本文件:
<?php
$lines = file("/cfg/customers.txt");
$top200 = array_slice(array_reverse($lines),0,200);
foreach($top200 as $line)
{
echo $line . "<br />";
}
?>
但是当页面第一次加载时,我怎样才能恢复到原来的字段呢?那么,如何将读取例程合并到上述代码中呢?