簡體   English   中英

將 HTML 表中的多行輸入作為單獨的行寫入 CSV 文件?

[英]Write multiple rows of input from HTML table into a CSV file as individual rows?

我創建了一個 HTML 表,我想用它來收集值並將它們存儲在 CSV 文件中。 HTML 代碼如下所示:

<form method="post">

<table>
  <tbody>
    <tr>
        <th></th>
        <th>1</th>
        <th>2</th>
        <th>3</th>
    </tr>
    <tr>
        <th>A</th>
        <td><input type="text" class="form-control" name="A1" /></td>
        <td><input type="text" class="form-control" name="A2" /></td>
        <td><input type="text" class="form-control" name="A3" /></td>
    </tr>
    <tr>
        <th>B</th>
        <td><input type="text" class="form-control" name="B1" /></td>
        <td><input type="text" class="form-control" name="B2" /></td>
        <td><input type="text" class="form-control" name="B3" /></td>
    </tr>
    <tr>
        <th>C</th>
        <td><input type="text" class="form-control" name="C1" /></td>
        <td><input type="text" class="form-control" name="C2" /></td>
        <td><input type="text" class="form-control" name="C3" /></td>
    </tr>
  </tbody>
</table>

<input type="submit" name="submit" value="Submit" />

</form>

I plan on having the PHP code in the same web page file, prior to the HTML code being called, to perform the action of taking the input and putting it into a CSV file. 像這樣的事情開始...

<?php

$A1 = '';
$A2 = '';
$A3 = '';
$B1 = '';
$B2 = '';
$B3 = '';
$C1 = '';
$C2 = '';
$C3 = '';

function clean_text($string)
{
 $string = trim($string);
 $string = stripslashes($string);
 $string = htmlspecialchars($string);
 return $string;
}

if(isset($_POST["submit"]))
{

   $A1 = clean_text($_POST["A1"]);
   $A2 = clean_text($_POST["A2"]);
   $A3 = clean_text($_POST["A3"]);
   $B1 = clean_text($_POST["B1"]);
   $B2 = clean_text($_POST["B2"]);
   $B3 = clean_text($_POST["B3"]);
   $C1 = clean_text($_POST["C1"]);
   $C2 = clean_text($_POST["C2"]);
   $C3 = clean_text($_POST["C3"]);      

   $file_open = fopen("input.csv", "a");

...但我被困在這一點上。 我猜“fputcsv”會是我的朋友,但我不確定如何應用它。 該表代表三行三列; 我的目標是將三行添加到 CSV 文件中,以逗號分隔。 就像是...

A1,A2,A3
B1,B2,B3
C1,C2,C3

我怎樣才能做到這一點?

以下邏輯應該可以幫助您:

<?php
$A1 = $A2 = $A3 = $B1 = $B2 = $B3 = $C1 = $C2 = $C3 = '';

function clean_text($string)
{
    $string = trim($string);
    $string = stripslashes($string);
    $string = htmlspecialchars($string);
    return $string;
}

// store form data (records: A1-A3, B1-B3 and C1-C3) in array $cleanFormVars 
$i = 0;
$j = 0;
if (isset($_POST["submit"])) {
    $formVars = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
    foreach($formVars as $var) {
        $cleanFormVars[$i][] = clean_text($_POST[$var]);
        $j++;
        if($j % 3 === 0) $i++; // create a record(row) for A1-A3, B1-B3, and C1-C3
    }
}

// write the records(rows) in array $cleanFormVars to .csv
$file_open = fopen("input.csv", "a");
foreach($cleanFormVars as $fields) {
    fputcsv($file_open, $fields);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM