我不是贸易开发商或仅使用<--
免责声明
所以,我有一个带有几百个输入的html表单,实质上是一个有限的模拟电子表格。
表格是3列乘150行。
为了演示,我们假设表单/表结构如下所示:
<table>
<tr>
<td><input name="this_row_1" /><input name="that_row_1" /><input name="the-other_row_1" /></td>
</tr>
<tr>
<td><input name="this_row_2" /><input name="that_row_2" /><input name="the-other_row_2" /></td>
</tr>
<tr>
<td><input name="this_row_3" /><input name="that_row_3" /><input name="the-other_row_3" /></td>
</tr>
<!-- ( . . . ) -->
<tr>
<td><input name="this_row_150" /><input name="that_row_150" /><input name="the-other_row_150" /></td>
</tr>
</table>
当表单发布到PHP时,我正在寻找正确的语法来在循环中分配处理程序(右词?)。
我对编码知之甚少,但我一直在玩和测试这种变体/概念:
for ($i = 1; $i < $_POST['[name="this_row_' + i + '"]']; $i++)
{
echo $_POST['[name="this_row_' + i + '"]'][$i] + "," + $_POST['[name="that_row_' + i + '"]'][$i] + "," + $_POST['[name="the-other_row_' + i + '"]'][$i];
}
但显然它不起作用,如果还没有暗示,我想写一个新的CSV文件,其中列标题作为最终游戏。
感谢任何帮助。
答案 0 :(得分:0)
试试这个。
$file=fopen(<DIRECTORY_PATH>."file.csv","w");
if(!$file){
//error
}
$this_row=$_POST['this_row'];//consider sanitizing/cleaning the data
$that_row=$_POST['that_row']; //consider sanitizing/cleaning the data
$the_other_row=$_POST['the_other_row']; //consider sanitizing/cleaning the data
$loop=count($this_row); // assuming count of all the other columns are same
$csv_data="";
for($i=0;$i<$loop; $i++){
$csv_data.=$this_row[$i].", ".$that_row[$i].", ".$the_other_row[$i].", ";
//remove the last comma
$csv_data.="\n";
}
}
fwrite($file,$csv_data);
答案 1 :(得分:0)
让我们从您的标记开始。
如果您有多个相同的字段(即应该在数字中使用相同的名称,例如您的示例)。您应该将它们命名为name="whateverNameYouWant[]"
(是的,所有这些都使用相同的名称)。这将导致PHP将这些输入作为输入数组,并且您将能够像$_POST["whateverNameYouWant"][42]
那样访问它们。
当你这样做时,一切都会变得更简单。
对于CSV,PHP有 functions 来处理CSV文件。现在,您可以轻松地将$ _POST变量循环并访问$_POST["this_row"][$i]
等等。