Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从文件中读取文本,然后填充字段_Php - Fatal编程技术网

PHP从文件中读取文本,然后填充字段

PHP从文件中读取文本,然后填充字段,php,Php,我有一个页面,其中显示了一些可以通过以下代码保存到文件中的字段: <?php if($_POST['Submit']) { $company = $_POST['company']; $address1 = $_POST['address1']; $address2 = $_POST['address2']; $address3 = $_POST['address3']; $address4 = $_POST['address4']; $contact = $_POST['contact

我有一个页面,其中显示了一些可以通过以下代码保存到文件中的字段:

<?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">&nbsp;</p>
</form>
ENDOFTXT;
}
?>

现在,我可以使用以下代码读取文本文件:

<?php
$lines = file("/cfg/customers.txt");
$top200 = array_slice(array_reverse($lines),0,200);
foreach($top200 as $line)
{
    echo $line . "<br />";
}

?>

但是,当页面第一次加载时,如何将其放回原始字段?
那么,如何将读取例程合并到上述代码中?

我没有真正理解您的问题。你能更详细一点吗?你能把整个代码,而不仅仅是这一小段吗?我们需要了解为什么不使用数据库来存储这些数据?我需要的是一个包含一些数据的文本文件。然后是一个PHP页面,将此数据加载到spcific文本框中。然后,我可以将数据更改为文本框,并使用submit按钮将其再次保存到文本文件中,就像第一个代码所做的那样。因此,第一个代码非常适合保存数据,但是当页面启动时,如何加载txt文件并将数据放入字段中呢?