![](/img/trans.png)
[英]How to check for Array in Database CodeIgniter and if they don't exist Add them
[英]How to add Array to database in codeigniter?
我有一個帶有多選字段的表單。 我為每個公司選擇3個類別,但僅將一個類別添加到數據庫中。
如何將3個類別的數組添加到數據庫中? 我使用聯接表將多個類別添加到公司。
我的表結構:
companies
---------
companyid
companyname
etc etc
categories
---------
categoryid
categoryname
companycategories
----------------
companycategoryid
categoryid
companyid
我的控制器:
function update()
{
$id = $this->uri->segment(3);
$data = 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'),
);
if($this->input->post('logo')) { $data['logo'] = $this->input->post('logo'); }
$this->members_model->updatebedrijf($id, $data);
$b = $this->session->userdata('idbedrijven');
redirect("members/$b");
}
我的模特:
function updatebedrijf($id, $data)
{
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
$to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat1($to_bedrijfcategorieen2);
}
function insert_bedrijfcat1($data1)
{
echo '<pre>';
print_r($data1);
echo '</pre>';
$id = $this->uri->segment(3);
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijfcategorieen', $data1);
return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
}
我的表格:
<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_multiselect('categorieen[]', $opties, key($selectie)); ?></td>
</tr>
print_r($ data1);的輸出 給我這個:
Array
(
[idcategorieen] => Array
(
[0] => 11
[1] => 12
[2] => 13
)
)
希望清楚。
在這里Update one to many relation
是技術
function insert_bedrijfcat1($data1)
{
$delete = $this->db->query("DELETE FROM `bedrijfcategorieen` WHERE `idbedrijven` = '" . $id. "'");
$id = $this->uri->segment(3);
foreach($data1['idcategorieen'] as $cat_id){
$this->db->query("INSERT INTO `bedrijfcategorieen` (`idbedrijven`,`idcategorieen` ) VALUES ('".$id."','".$cat_id."')");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.