对于php中不执行一次以上的每个循环


For each loop not executing more than once in php

表单中有两个字段,一个是姓名,另一个是电子邮件,就像这样

<td class="left"><input style="  width: 30%; text-align: center;" type="text" name="name[]" value="<?php echo $geo_zone['name']; ?>" /></td>
<td class="left"><input style="  width: 30%; text-align: center;" type="email" name="email[]" value="<?php echo $geo_zone['email']; ?>" /></td>

有名称数组类型,以便我可以在运行时存储多个值,因为我的表单是在运行时生成的,根据数据库结果可能有多个名称和电子邮件。

在我的控制器我只是获取这些值在print_r()我得到写结果。但是当我执行我的for each循环时,它只运行一次,并在数据库中插入最后一行。

$name= $this->request->post['name'];
$email= $this->request->post['email'];
foreach( $name as $key => $n ) {
$this->data['geo_zone_id']=$this->model_module_shipping_pools->Get_geo_zone_id($n);
$geo_zone_id=$this->data['geo_zone_id'];
$geo_zone_id=$geo_zone_id[0]['geo_zone_id'];
$this->model_module_shipping_pools->drop_data();
$mail=$email[$key];
$this->model_module_shipping_pools->insertData($geo_zone_id,$mail);

我不知道为什么它只运行一次。我在openart工作

您需要多个具有相同数组名称的输入标记来获得数组值!

例如(我不知道你如何生成原始html),你可以有:

<td class="left"><input style="  width: 30%; text-align: center;" type="text" name="name[]" value="<?php echo $geo_zone['name'][0]; ?>" /></td>
<td class="left"><input style="  width: 30%; text-align: center;" type="text" name="name[]" value="<?php echo $geo_zone['name'][1]; ?>" /></td>
...

查看如何在这里使用数组的示例