[英]How to insert id of some table into another in codeigniter
Im doing insert into 2 tables in first table im inserting this array 我正在插入第一个表的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
}
and in second one im inserting this array 在第二个即时消息中插入此数组
array(2) {
["property_type_id"]=>
string(2) "21"
["alt_txt"]=>
string(7) "4666.45"}
and this is my function in controller for insert 这是我在控制器中用于插入的功能
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);
and here is how my tables looks like 这是我的桌子的样子
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
So what im want to do is to insert property_id also in second table together with second array 所以我想做的就是在第二个表中同时插入property_id和第二个数组
use the codeigniter return $this->db->insert_id()
or $get_id = $this->db->insert_id
使用codeigniter返回
$this->db->insert_id()
或$ get_id = $this->db->insert_id
Here is how i get id from another table: 这是我从另一个表获取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.