简体   繁体   English

在PHP中遍历多个数组

[英]looping through multiple arrays in php

I want to insert three protocols in a table "service_providers" under "protocol" field and three plateforms in the same table under "plateform" field, when i click on submit button three records should be inserted in "service_providers" table. 我想在“协议”字段下的表“ service_providers”中插入三个协议,并在“平台”字段下的同一表中插入三个平台,当我单击提交按钮时,应在“ service_providers”表中插入三个记录。

admin/protocol" method="post"> Protocols Plateform admin / protocol“ method =” post“>协议平台

this is the action code 这是动作代码

 <?php 

                  $protocols = $this->input->post('protocol');
                  $plateform     = $this->input->post('plateform');
                    foreach($protocols as $protocol){
                          foreach($plateforms as $plateform){
                                $data= array(
                                    'protocols' => $protocol,
                                    'plateform' => $plateform
                                    );  


                          }
                $this->db->insert('service_providers',$data);
                    }


?>

when i code like that six records insert , which is wrong 当我编写类似六个记录的代码时,这是错误的

id       protocol      plateform     service_id 
1      PPTP      Windows      100
2     OpenVPN    Andriod      100
3     LPTP       iOS           100

when i code like that three records insert , which is right but inner foreach loop's values do not insert how to tackle this i want like that in database 当我编写类似这三个记录的代码insert,这是正确的,但是内部foreach循环的值不插入如何解决此问题时,我想要在数据库中那样

 id protocol plateform service_id 1 PPTP Windows 100 2 OpenVPN Andriod 100 3 LPTP iOS 100 

Try this code in your action : This will work only if protocol and plateform having number of parameters. 在您的操作中尝试以下代码:仅当协议和平台具有多个参数时,此方法才有效。

$protocols = $this->input->post('protocol');
$plateform     = $this->input->post('plateform');
$k =0;

foreach($protocols as $protocol){

        $data= array(
        'protocols' => $protocol,
        'plateform' => $plateform[$k]
        );  
        $this->db->insert('service_providers',$data);
        $k++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM