0

我正在编写一个基于上传的 CSV 文件写出表单的脚本。CSV 文件包含由仅包含“,,,,”的行分隔的部分。每个块都包含有关将要启动的服务器的信息。它看起来像这样:

主机,tctivt2r6ra03,,,
int,eth0,10.153.196.248,255.255.255.0,10.153.196.1
整数,eth1,10.153.157.113,255.255.255.128,10.153.157.1
int,eth2,10.153.157.241,255.255.255.128,10.153.157.129
int,eth3,00:50:56:ac:69:cb,,
部分,/家,10,,
部分,交换,10,,
部分,/opt,60,,
部分,/数据,30,,
,,,,

此输入被写入包含表的 div。每行输入都是表中的唯一行。一些值最终出现在文本框中。

该脚本生成如下所示的输出。

每个盒子都是动态生成的,由从 0 开始的主机 ID 和同样从 0 开始的盒子编号组成。例如:

<tr><th>eth0: </th><td><input type="text" value="10.153.196.248" name="host0Box0" /></td><td><input type="text" value="255.255.255.0" name="host0Box1" /></td><td><input type="text" value="10.153.196.1" name="host0Box2" /></td></tr><tr>

我的问题是如何处理具有可变数量框的表单。具体来说,在处理输入的“部分”行时,可能有六个或更多框。

具有四个分区的示例:

<tr><th>Partition name:</th><td> /</td><td>10</td></tr>
<tr><th>Partition name:</th><td> /var</td><td><input type="text" value="10" name="host0Box9" /></td></tr>
<tr><th>Partition name:</th><td> /home</td><td><input type="text" value="10" name="host0Box10" /></td></tr>
<tr><th>Partition name:</th><td> swap</td><td><input type="text" value="10" name="host0Box11" /></td></tr>
<tr><th>Partition name:</th><td> /opt</td><td><input type="text" value="60" name="host0Box12" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/data" name="host0Box13" />/td><td><input type="text" value="30" name="host0Box14" /></td></tr>
<tr><th>Number of Disks: </th><td><input type="text" value="1" name="host0Disks" /></td></tr></table></div><span><br /><br /></span>

六个分区的示例:

<tr><th>Partition name:</th><td> /</td><td>10</td></tr>
<tr><th>Partition name:</th><td> /var</td><td><input type="text" value="10" name="host1Box9" /></td></tr>
<tr><th>Partition name:</th><td> /home</td><td><input type="text" value="10" name="host1Box10" /></td></tr>
<tr><th>Partition name:</th><td> swap</td><td><input type="text" value="10" name="host1Box11" /></td></tr>
<tr><th>Partition name:</th><td> /opt</td><td><input type="text" value="60" name="host1Box12" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/data" name="host1Box13" /></td><td><input type="text" value="30" name="host1Box14" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/test1" name="host1Box15" /></td><td><input type="text" value="30" name="host1Box16" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/test2" name="host1Box17" /></td><td><input type="text" value="20" name="host1Box18" /></td></tr>
<tr><th>Number of Disks: </th><td><input type="text" value="1" name="host0Disks" /></td></tr></table></div><span><br /><br /></span>

这是一项正在进行的工作,我知道为了正确处理每个主机部分,我将不得不进行更改。与此同时,我正在尝试解决这一问题。

我现在的问题是,当 II 最终到达处理表单的地步时,当填充 $_POST 数组时,我应该使用什么方法来说明动态分配的项目名称?我是否将数组值拉出并将它们放在不同的数组中?我可以像普通数组一样迭代 $_POST 数组吗?还有其他我不知道的选择吗?

4

2 回答 2

2

看起来你在追求这个?

因此,不要name="host0Box0"使用name="host[0][Box][]假设您可以更改脚本输出。

使用[]将自动填写一个数组,例如

<input type="text" name="foo[]" value="Hello" />
<input type="text" name="foo[]" value="World" />

会给你数组

Array(
  [foo] => Array(
    [0] => Hello
    [1] => World
  )
)

我可能误解了这个问题。

于 2012-11-08T02:57:59.970 回答
0

您可以像遍历任何数组一样遍历超全局 $_POST 数组。试试 var_dump($_POST) 看看有什么可用的。

由于发布的名称是未知的,如果这些名称包含不寻常的字符,您可能需要解析原始输入流。问题 813487的已接受答案可能会有所帮助。

于 2012-11-08T02:49:03.537 回答