0

这就是这种情况。我有一个程序从数组中获取数据并将其显示在表格上。我创建了允许客户端输入新项目的文本输入类型,当单击提交按钮时,所有字段都将添加到表中。我在想是否有办法将数组写入文本文件并执行 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>
4

1 回答 1

2

如果您必须使用文本文件而不是数据库,那么最好的方法是将表格行数据附加到CSV格式的文件中。

脚本的第一部分应处理表单输入,最好验证每个字段,但以最简单的形式,您可以从 POST 数据创建一个数组。然后您可以使用fputcsv()写入文件,并使用 fgetcsv() 将 CSV 数据放入一个数组中,以便您可以在表格中显示它。

<?php

/**
 * Ensure you have the full path to the file on the file system
 */
$myFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'file.txt';

/**
 * This section handles the form submit
 */

// Create a boolean value checking the necessary data is posted
$check = isset(
  $_POST['product_name'],
  $_POST['product_description'],
  $_POST['price'],
  $_POST['image']
);
// Do we have the data?
if ($check) {

  // Open the file to append a new row
  $fh = fopen($myFile, "a");

  // Create an array of data from the POSTed vars
  $dataRow = array(
    $_POST['product_name'],
    $_POST['product_description'],
    $_POST['price'],
    $_POST['image']
  );

  // Write the data to file in CSV
  fputcsv($fh, $dataRow);

  // Close the file
  fclose($fh);

}

/**
 * Then we re-open the file for reading
 */
$fh = fopen($myFile, "r");

/**
 * The next section displays the table
 */
?>
  <!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
  </head>
  <body>

  <form action="/" method="post">
    <p>
      <label for="product_name">Product Name</label>
      <input id="product_name" name="product_name" type="text"/>
    </p>
    <p>
      <label for="product_description">Product Description</label>
      <input id="product_description" name="product_description" type="text"/>
    </p>
    <p>
      <label for="price">Price</label>
      <input id="price" name="price" type="text"/>
    </p>
    <p>
      <label for="image">Image</label>
      <input id="image" name="image" type="text"/>
    </p>
    <input type="submit" value="Submit"/>
  </form>

  <table border="1" align="center" cellpadding="10">
    <tbody>
    <thead>
    <tr>
      <th>Product Name</th>
      <th>Product Description</th>
      <th>Price</th>
      <th>Image</th>
    </tr>
    </thead>
    <tbody>
    <?php
    /**
     * Here you can loop through each row in the file, getting an array of
     * data from the CSV values
     */
    $data = fgetcsv($fh);

    ?>
    <?php while (($data = fgetcsv($fh)) !== false) : ?>
      <tr>
        <td><?php echo htmlentities($data[0]); ?></td>
        <td><?php echo htmlentities($data[1]); ?></td>
        <td><?php echo htmlentities($data[2]); ?></td>
        <td><?php echo htmlentities($data[3]); ?></td>
      </tr>
    <?php endwhile; ?>
    </tbody>
    </tbody>
  </table>

  </body>
  </html>
<?php
/**
 * Finish off by closing the file
 */
fclose($fh);
?>
于 2013-10-31T10:09:27.487 回答