【问题标题】:Codeigniter approach to save all posted values in bidimensional array将所有发布的值保存在二维数组中的 Codeigniter 方法
【发布时间】:2023-03-14 04:39:01
【问题描述】:

我有一个多表单,我需要向 4 个用户、新闻、提要、纬度(可能是 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> 

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

控制器事件.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 ,


     }

} 

是否有必要使用内爆之类的,有没有更好的方法来做这个多次插入?

【问题讨论】:

  • 我在这个设计的一开始并没有看到太多的意义。你确定你已经看透了替代方案吗?例如,我认为使用键会更合乎逻辑。 array('person' =&gt; array('name' =&gt; $name, 'address' =&gt; $address))

标签: php mysql codeigniter multidimensional-array


【解决方案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...
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我确实为每个实体都有一个模型,甚至它们对应的 crud,这里的问题是我需要将数据插入到一个表中,但是这个表依赖于其他几个,这就是为什么只需要一个表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2020-12-04
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多