0

参加するために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;
}
4

1 に答える 1