我正在使用 3 个表来加入。我有一个可以添加工厂的表格。我还可以为该特定工厂选择一个类别。我使用下表:
factorycategories
-----------------
idfactorycategories
idfactories
idcategories
(in this table i use a join to show the categories and factories on one page)
factories
---------
idfactories
factoryname
address
country
telephone
...
...
categories
----------
idcategories
category
我想在我的 factorycategories 表中插入最后插入的 idcategories 和 idfactories。我怎样才能做到这一点?
我插入表单值的模型:
function addbedrijf()
{
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $this->input->post('logo')
);
$this->db->insert('bedrijven', $data1);
}
我在同一页面上显示类别和工厂的模型功能:
function get_result($idbedrijven)
{
$this->db->where('bedrijven.idbedrijven', $idbedrijven);
$this->db->join('bedrijven', 'bedrijfcategorieen.idbedrijven = bedrijven.idbedrijven');
$this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen');
$this->db->group_by('Categorie', 'idbedrijven', 'idcategorieen');
$result = $this->db->get('bedrijfcategorieen', 1);
return $result->result();
}
希望我为你们提供了足够的代码。
编辑:
到目前为止,我使用以下示例的代码:
function addbedrijf()
{
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $this->input->post('logo')
);
$this->db->insert('bedrijven',$data1);
if($this->db->affected_rows() >= 1)
{
$this->insert_bedrijfcat($this->db->insert_id);
}else{
return FALSE
}
}
function insert_bedrijfcat($id)
{
$this->db->insert('bedrijfcategorieen',array('idbedrijven'=>$id));
return $this->db->affected_rows() ?= 1 ? TRUE : FALSE;
}