使用PHP

时间:2018-03-26 13:58:07

标签: php csv

我想从表格中的每一行( oldc,newc,skey )获取表格输入,并将它们作为一行插入CSV文件中。
据我所知,他们已经在一个阵列?那么有一种简单的方法可以在我的PHP中使用 foreach循环 (我的当前循环已损坏)吗?

我对编程知之甚少,所以很清楚答案。 感谢。

的index.html

<form class="form-inline" id="main-form" action="process.php" method="post"> 
            <div class="input-group mb-3">
                <input required name="username" class="form-control" type="text" value="a" maxlength="4" placeholder="Enter name(Max 4 Characters)"> 
                    <div class="input-group-append">
                        <button class="btn btn-primary" type="submit" >Save to server</button>
                    </div>
            </div>
            <table id="myTable" class="table table-hover table-dark table-striped table order-list">
                <tbody>
                    <tr>
                        <td><input  name='oldc[]' type="text" class="form-control" value="b" maxlength='17' required placeholder='W7 SN' /></td>
                        <td><input  name='newc[]' type="text" class="form-control" value="c" maxlength='17' required placeholder='W10 SN' /></td>
                        <td><input  name='skey[]' type="text" class="form-control" value="d" maxlength='10' required placeholder='Security Key' /></td>
                        <td><button name='del'  type="button" class="btn btn-danger invisible" >Delete</button></td>
                    </tr>
                </tbody>

scripts.js中

$("#addrow").on("click", function () {
    var newRow = $("<tr>");
    var cols = "";


    cols += '<td><input type="text"   class="form-control" placeholder="W7 SN"        name="oldc[]' + counter + '"/></td>';
    cols += '<td><input type="text"   class="form-control" placeholder="W10 SN"       name="newc[]' + counter + '"/></td>';
    cols += '<td><input type="text"   class="form-control" placeholder="Security Key" name="skey[]' + counter + '"/></td>';

    cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger"           value="Delete"></td>';
    newRow.append(cols);
    $("table.order-list").append(newRow);
    counter++;
    i++;

});

process.php

$oldc = $_POST["oldc"];
$newc = $_POST["newc"];
$skey = $_POST["skey"];
$header = 'W7 SN, W10 SN, Security Key';
$data = $header . "\n" . $oldc . "," . $newc . "," . $skey . "\n";

$myfile = fopen($location, 'w') or die("Unable to open file!");

foreach ($data as $row){
fputcsv($myfile, $row }
);


fclose($myfile);

2 个答案:

答案 0 :(得分:0)

您的代码存在多个问题。

  • 您的代码中存在语法错误。 )
  • 附近缺少}
  • $data不是数组。

替换

$header = 'W7 SN, W10 SN, Security Key';
$data = $header . "\n" . $oldc . "," . $newc . "," . $skey . "\n";

以下

$header = 'W7 SN, W10 SN, Security Key';
$data = array(
        'W7 SN'=> $oldc,
        'W10 SN'=> $newc,
        'Security Key'=>$skey
       );


fputcsv($myfile, array('W7 SN', 'W10 SN', 'Security Key'));

// put foreach here

答案 1 :(得分:0)

由于传入的数据本身就是数组,因此您无法以某种方式将它们组合在一起,然后希望fputcsv将它们排序。

我所做的是假设您的输入将包含来自表单的oldc,newc和skey值数组。代码循环遍历oldc值,并从每个其他数组中选择相应的值......

$oldc = $_POST["oldc"];
$newc = $_POST["newc"];
$skey = $_POST["skey"];
$header = 'W7 SN, W10 SN, Security Key';
$header = explode(",", $header);

$myfile = fopen($location, 'w') or die("Unable to open file!");
fputcsv($myfile, $header );
foreach ($oldc as $key => $value){
    $row = [ $value, $newc[$key], $skey[$key]];

    fputcsv($myfile, $row);
}
fclose($myfile);