我在选择下拉菜单中使用多个选择,我想将其存储在数据库中。
我听说内爆爆炸,但我不知道如何使用它们。请告诉我如何在codeigniter中内爆和爆炸。
我的HTML表单:
<div class="form-group">
<label>
Please Select Proof Of ID Deposit
</label>
<select multiple="" name="oids" class="js-example-basic-multiple js-states form-control">
<optgroup>
<option value="Passport">Passport</option>
<option value="National ID Card">National ID Card</option>
<option value="Driving Licence">Driving Licence</option>
<option value="Univesity ID">Univesity ID</option>
</optgroup>
</select>
</div>
我的控制器:
public function customer_insert(){
$data = array(
'id'=>$this->input->post('up_id'),
'Other_id'=>$this->input->post('oids') //here I want to implode my values
)
$this->db->insert('customer', $data);
}
查看文件:
<table>
<thead>
<tr><td>ID</td></tr>
<tr><td>Other ID</td></tr>
</thead>
<?php foreach ($query as $customer){?>
<tr><td><?php echo $customer->id?></td></tr>
<tr><td><?php echo $customer->Other_id?></td></tr> // I want to show my imploded value here.
<?php}?>
</table>
答案 0 :(得分:3)
首先在您的 HTML FORM 中,您需要使用数组更改name
attr值,除非它不会生成所选值的数组
<select multiple="" name="oids" class="js-example-basic-multiple js-states form-control">
必须像
<select multiple="" name="oids[]" class="js-example-basic-multiple js-states form-control">
在控制器功能
中public function customer_insert() {
$oidVal = implode(",", $this->input->post('oids'));// Converted array into comma separated value using implode
$data = array(
'id'=> $this->input->post('up_id'),
'Other_id'=> $oidVal
)
$this->db->insert('customer', $data);
}
并在查看文件
中<table>
<thead>
<tr><td>ID</td></tr>
<tr><td>Other ID</td></tr>
</thead>
<?php foreach ($query as $key => $value) { ?>
<tr><td><?php echo $value['id']; ?></td></tr>
<tr><td><?php echo explode(',',$value['Other_id']);?></td></tr> // exploded value
<?php } ?>
</table>
答案 1 :(得分:0)
您可以使用implode函数通过字符串和explode函数连接数组元素,以按字符串分割字符串。在这种情况下,您可以像这样使用它们。
public function customer_insert() {
// Convert array to comma separated value
$oidString = implode(",", $this->input->post('oids'));
$data = array(
'id'=> $this->input->post('up_id'),
'Other_id'=> $oidString //here I want to implode my values
)
$this->db->insert('customer', $data);
}
如果要从逗号分隔值中取回数组,可以使用explode
。
// Convert comma separated value to array
$otherId = explode(",", $customer->Other_id);
希望它对你有用。
答案 2 :(得分:0)
public function insert()
{
//Insert second stage details for employer into database.
$Specilized_category = $this->input->post('spec_cat');
$data=array(
'Specilized_category'=>json_encode(implode(",", $Specilized_category)),
);
$this->db->insert('tbl_employer', $data);