簡體   English   中英

如何在Codeigniter中將某個表的ID插入另一個表

[英]How to insert id of some table into another in codeigniter

我正在插入第一個表的2個表中,我正在插入此數組

   array(2) {
  [0]=>
  array(5) {
    ["language_id"]=>
    string(1) "3"
    ["property_id"]=>
    int(82)
    ["option_id"]=>
    string(2) "10"
    ["value"]=>
    string(0) ""
    ["value_num"]=>
    NULL
  }

在第二個即時消息中插入此數組

array(2) {
["property_type_id"]=>
string(2) "21"
["alt_txt"]=>
string(7) "4666.45"}

這是我在控制器中用於插入的功能

    public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 

    // Insert all
    $insert_batch = array();
    foreach($data as $key=>$value)
    {
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);

            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }

            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);

            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;


        }
    }

                        $data=$this->input->post('data');
    if(count($insert_batch) > 0)
        $this->db->insert_batch('property_value', $insert_batch); 
    if($this->db->affected_rows() > 0)
        $this->db->insert_batch('property_type_details', $data);

這是我的桌子的樣子

Field Type
id int(11) NOT NULL
language_id int(11) NOT NULL
property_id int(11) NOT NULL
option_id int(11) NOT NULL
value text NULL
value_num int(11) NULL

Field Type
id int(10) unsigned NOT NULL
property_id int(10) NULL
property_type_id int(10) NULL
alt_txt double(10,2) NULL

所以我想做的就是在第二個表中同時插入property_id和第二個數組

使用codeigniter返回$this->db->insert_id()或$ get_id = $this->db->insert_id

這是我從另一個表獲取ID的方法:

public function addBanner() {
    $data = array('name' => $this->input->post('name'),'status' =>   $this->input->post('status'));
    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert('banner');

    $banner_id = $this->db->insert_id();

    $data = array(
        'banner_id' => (int)$banner_id,
        'image' => $data_image['file_name'],
        'link' => $link
    );


    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert('banner_image');
    }
}

return $banner_id;
}
public function save_dynamic($data, $id)
{
    // Delete all
    $this->db->where('property_id', $id);
    $this->db->where('value !=', 'SKIP_ON_EMPTY');
    $this->db->delete('property_value'); 

    // Insert all
    $insert_batch = array();
    foreach($data as $key=>$value)
    {
        if(substr($key, 0, 6) == 'option')
        {
            $pos = strpos($key, '_');
            $option_id = substr($key, 6, $pos-6);
            $language_id = substr($key, $pos+1);

            $val_numeric = NULL;
            if( is_numeric($value) )
            {
                $val_numeric = intval($value);
            }

            $insert_arr = array('language_id' => $language_id,
                                'property_id' => $id,
                                'option_id' => $option_id,
                                'value' => $value,
                                'value_num' => $val_numeric);

            if($value != 'SKIP_ON_EMPTY')
                $insert_batch[] = $insert_arr;


        }
       $data=$this->input->post('data');
       $this->db->insert('property_value', $insert_batch);
       $data['property_id']=$this->db->insert_id();
       $this->db->insert('property_type_details', $data);
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM