这就是这种情况。我有一个程序从数组中获取数据并将其显示在表格上。我创建了允许客户端输入新项目的文本输入类型,当单击提交按钮时,所有字段都将添加到表中。我在想是否有办法将数组写入文本文件并执行 fwrite 将客户端输入写入文本文件。我还注意到文本文件不断被覆盖,因此输入的数据不会保留,而不是
$handle=fopen($myfile, "w");
我应该使用
$handle=fopen($myfile, "a");
如果不存在文件,是否会修改工作,如果不存在,它将创建文本?
当我运行部分借用和创建的代码时,客户端新表不会添加到已经存在的表中。
抱歉,如果不清楚,我是新手。任何帮助将不胜感激,如果您需要更多信息,请告诉我。
这是我到目前为止开发的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<center>
<?php
$myfile = "file1.txt";
if(!empty($_POST['product_name']))
{
$newData=nl2br(htmlspecialchars($_POST['ta']));
//////////////////////////////////////////////////
//the following variables are grabbing specific information from the form.
$product_name = array($_POST['product_name']);
$product_descriptoin = array($_POST['product_description']);
$price = array($_POST['price']);
$image = array($_POST['image']);
echo '<table border="1" align="center" cellpadding="10">';
echo "<tr align='center'>
<td><b>Product Name</b></td>
<td><b>Product Description</b></td>
<td><b>Price</b></td>
<td><b>Image</b></td>
</tr>";
foreach ($product_name as $key=>$newData) //this is suppose to add client input
//to a table.
{
echo "<tr align='center'>";
echo '<td>';
echo $product_name[$key];
echo '</td>';
echo '<td>';
echo $product_descriptoin[$key];
echo '</td>';
echo '<td>';
printf('$%', $price);
echo $price[$key];
echo '</td>';
echo '<td>';
echo "<img src='".$image[$key]."' width='200' height='300' align='center'>";
echo '</td>';
}
echo '<tr>';
echo '<td>';
echo '<td>';
echo '<td>';
echo '<td>';
echo '</td>';
echo '</td>';
echo '</td>';
echo '</td>';
echo '</tr>';
echo '</table>';
// /////////////////////////////////////////////
$handle=fopen($myfile, "w");
fwrite($handle, $newData);
fclose($handle);
}
?>
<?php
if (file_exists("$myfile"))
{
$myData= file_get_contents($myfile);
}
?>
<form action ="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000000000" />
<h1>Product Information Page</h1>
<p>Product Name: <input type="text" name="product_name" /></p>
<p>Upload Product Image: <input type="text" name="image" id="image"</p>
<p>Description: <input type="text" name="product_description" /></p>
<p>Price: <input type="text" name="price" /></p>
<br/><br/>
<input name="submit_btn" type="submit" />
</form>
<br/>
<br/>
<br/>
<?php echo $myData; ?>
<?php
//Orlando's mini assignment 3
//arrays that include pictures and all information
$product1 = array('Product Name'=> 'Beauty Boxer Shorts', 'Product Image'=>'<img src= 001.jpg height="88" width="110">', 'Description' => 'Beauty Boxer Shorts', 'Price (each)' => '$14.95');
$product2 = array('Product Name'=> 'Girl Generation Shorts', 'Product Image'=>'<img src= 002.jpg height="88" width="110">', 'Description' => 'Girl Generation Shorts', 'Price (each)' => '$15.95');
$product3 = array('Product Name'=> 'Pick Shorts', 'Product Image'=>'<img src= 003.jpg height="88" width="110">', 'Description' => 'Pick Shorts', 'Price (each)' => '$22.95');
$product4 = array('Product Name'=> 'Red Bull Shorts', 'Product Image'=>'<img src= 004.jpg height="88" width="110">', 'Description' => 'Red Bull Shorts', 'Price (each)' => '$24.95');
$product5 = array('Product Name'=> 'White with Gold <br> Dragon Shorts', 'Product Image'=>'<img src= 005.jpg height="88" width="110">', 'Description' => 'White with Gold <br> Dragon Shorts', 'Price (each)' => '$25.95');
// array of all products
$products = array($product1, $product2, $product3, $product4, $product5);
//setup an html table
echo "<table border=1>";
//prints the table header
echo "<tr>";
$header = array_keys($product1);
foreach ($header as $key => $head)
{
echo "<th>$head</th>";
}
echo "</tr>";
//iterate through the table body, printing each row
for($i=0; $i<count($products); $i++)
{
echo "<tr>";
foreach($products[$i] as $key => $value)
{
echo ("<td><center>$value</center></td>");
}
echo "</tr>";
}
?>
</body>
</html>
</center>