首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在codeigniter中将一些表的id插入到另一个表中

如何在codeigniter中将一些表的id插入到另一个表中
EN

Stack Overflow用户
提问于 2015-01-07 16:48:50
回答 2查看 2.7K关注 0票数 0

我在第一个表的两个表中插入了这个数组

代码语言:javascript
复制
   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
  }

在第二个示例中,im插入了以下数组

代码语言:javascript
复制
array(2) {
["property_type_id"]=>
string(2) "21"
["alt_txt"]=>
string(7) "4666.45"}

这是我在用于插入的控制器中的函数

代码语言:javascript
复制
    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);

下面是我的表格的样子

代码语言:javascript
复制
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也插入到第二个表和第二个数组中

EN

回答 2

Stack Overflow用户

发布于 2015-01-07 17:03:00

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

下面是我如何从另一个表中获取id:

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2015-01-07 17:04:08

代码语言:javascript
复制
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);
    }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27815520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档