2013-03-21 20 views
0

所有張貼的價值觀我有多種形式,我需要插入幾個值4個用戶,新聞,飼料,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> 

現在我想傳遞一個二維陣列,以事件模型,然後根據不同的值的插入到對應表

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


    } 

} 

是否有必要使用implode什麼的,有沒有更好的方式來做到這一點多重插入?

+0

在這個設計的開始我看不到那麼多的意思。你確定你已經看到了替代方案嗎?例如,我認爲使用密鑰會更合乎邏輯。 '陣列(「人」 =>陣列(「名」 => $名字,「地址」 => $地址))' – 2013-03-22 10:01:16

回答

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... 
} 
1

聽起來真的很複雜......爲什麼沒有每個實體的模型並將正確的字段傳遞給正確的模型?或者創建一個插入/更新每個模型的庫?

+0

我有每個實體模型,即使其對應的污物,這裏的問題是我需要插入數據到一個表,但這個表取決於其他幾個這就是爲什麼只需要一個表單。 – cMinor 2013-03-22 02:25:14