我正在使用codeigniter框架。我有一个id数组,要保存在我的表的service_id列中。我需要在我的表中存储一个id数组。我的表结构如下所示。
CREATE TABLE IF NOT EXISTS `addservice` (
`employee_id` int(10) NOT NULL,
`service_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我为此写了一个控制器:
class service extends CI_Controller
{
public function services()
{
$id = $this->session->userdata('employee');
$id_array = json_encode($this->input->post("services"));
$data_to_store=array('employee_id'=>$id,'service_id'=>$id_array));
$this->add_service_model->save($data_to_store);
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}
}
I used json_encode function. I dont get any error but i am getting a white screen. I wrote a view file where i post my id as check box.In this view file i am posting the service_id as a array and post them to my controller. My view file are:
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">Service id</th>
<th class="yellow header headerSortDown">Service name </th>
<th class="green header">Service catogary</th>
<th class="red header">Service tax</th>
<th class="red header">Service length</th>
<th class="red header">Service price</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($service as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['service_name'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['service_tax'].'</td>';
echo '<td>'.$row['service_length'].'</td>';
echo '<td>'.$row['service_price'].'</td>';
echo '<td class="crud-actions">
<input type="checkbox" value="'.$row['id'].'" name="services[]"/>
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
我的模型文件
class mymodel extends CI_Model{
public function get_addservices()
{
$this->db->select('*');
$this->db->from('addservice');
$query = $this->db->get();
return $query->result_array();
}
public function save($data)
{
$insert = $this->db->insert('addservice', $data);
return $insert;
}
}
在这个文件中,我使用了一个函数save来将数据保存到我的表中
答案 0 :(得分:1)
从输入帖子中删除json_encode
并使用for循环获取数组的值
<强>控制器强>
public function services()
$id = $this->session->userdata('employee');
$id_array = $this->input->post("services");
for ($i = 0; $i < count($id_array); $i++) {// add for loop here
if(isset($id_array[$i])&& $id_array[$i]!=""){
$data_to_store = array('employee_id' => $id, 'service_id' => $id_array[$i]);
$this->add_service_model->save($data_to_store);
}
}
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}