dongyinzhi4689 2013-03-21 23:33
浏览 28
已采纳

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

I have a multiform and I need to insert several values to 4 user,news,feed,lat (maybe 6) tables that are related by some ID this is because an event needs all this information.

my form:

<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> 

Now I would like to pass a bidimensional array to event model and then depending on value insert to corresponding table

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); 

    }

}

Now How to receive the array in model and do the insert how to declare 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 ,


     }

} 

Is it necessary to use implode or something, Is there a better way to do this multiple inserting?

  • 写回答

2条回答 默认 最新

  • dongxibo2095 2013-03-22 14:12
    关注
    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...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c