Codeigniter方法用于保存二维数组中的所有已发布值

时间:2013-03-21 23:33:53

标签: php mysql codeigniter multidimensional-array

我有一个multiform,我需要为4个用户,新闻,Feed,lat(可能是6个)表格插入一些ID,这是因为事件需要所有这些信息

我的表格:

<form method="post" action="<?php echo base_url();?>controller/save" name="event"/>
  <label>User Name</label>

   <!--#####FOR TABLE USER#####-->
  <input type="text" name="name">   
  <input type="text" name="name">



   <!--#####FOR TABLE NEWS#####-->
  <input type="text" name="title">   
  <input type="submit" value="body"/>


  <!--#######################
       OTHER TABLE FIELDS ...
   #######################
   -->    

   <input type="submit" value="save"/>

</form> 

现在我想将二维数组传递给事件模型,然后根据值插入到相应的表

controller event.php

class Event extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');   
        $this->load->model('event_model');     
    }

    public function save()
    {

    /** Is it possible to fill this bidimensional array with for loops???? **/

     $arr_data = array(     
          array("person", $this->input->post("name")    , $this->input->post("address")),
          array("news"  , $this->input->post("title")   , $this->input->post("body"  )),
          array("feed"  , $this->input->post("id_feed") , $this->input->post("main"  )),
          array("lat"   , $this->input->post("lat1"     , $this->input->post("lat"   ))
       ); 

        $this->event_model->insert_tables($arr_data); 

    }

}

现在如何在模型中接收数组并插入如何声明event_model?

class Event_model extends CI_Model {

    function Event_model ()
    {
        parent::__construct();
    }
     function insert_tables($arr_data) { 

        if( "person" )
          $this->db->insert(person_tb ,


     }

} 

是否有必要使用内爆或其他东西,有没有更好的方法来进行多次插入?

2 个答案:

答案 0 :(得分:1)

听起来真的很复杂......为什么没有每个实体的模型并将正确的字段传递给正确的模型?或者创建一个插入/更新每个模型的库?

答案 1 :(得分:1)

I would do something like this...

$person = array('name' => $this->input->post("name"), 'address' => $this->input->post("address"));
$news = array('title' => $this->input->post("title"), 'body' => $this->input->post("body"));
$feed = array('id' => $this->input->post("id_feed"), 'main' => $this->input->post("main"));
$lat = array('lat1' => $this->input->post("lat1"), 'lat' => $this->input->post("lat"));

if($this->person_model->insert($person)) {
    $person_id = $this->db->insert_id();
} else {
    // handle the problem of not having an id for this entity...
}

if($this->news_model->insert($news)) {
    $news_id = $this->db->insert_id();
} else {
    // handle the problem of not having an id for this entity...
}

if ($this->feed_model->insert($feed)) {
    $feed_id = $this->db->insert_id();
} else {
    // handle the problem of not having an id for this entity...
}

if($this->lat_model->insert($lat)) {
    $lat_id = $this->db->insert_id();
} else {
    // handle the problem of not having an id for this entity...
}