我无法将答案值中的数据从复选框发送到数据库中的选定行
我也在使用分页。
这是我的控制器:
function index() {
$this->load->library('pagination'); // pagination setting start from here
$config['base_url'] = site_url('dcm/index');
$config['per_page'] = 6;
$config['num_link'] = 5;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev' ;
$config['total_rows'] = $this->db->get('soal')->num_rows();
/* here we are the main problem */
$data['form_action'] = site_url('dcm/index');
$jawab = $this->input->post('jawab'); //<-- $jawab AS answer, this variable grab answer
$id_soal = $this->input->post('id_soal'); //<-- $id_soal AS ID, this variable just for posting ID
$this->dcm_model->inputJawab(); // <-- for sending data to Model
/* pagination */
$this->pagination->initialize($config);
$data['query'] = $this->db->get('soal', $config['per_page'], $this->uri->segment(3));
$data['link'] = $this->pagination->create_links();
$this->load->view('dcm/soal', $data); }
这是我的模型代码:
function inputJawab(){
$data = array();
if($this->input->post('jawab')){
$jawab = $this->input->post('jawab');
foreach($jawab as $each){
if(isset($each) && $each != ""){
$data['jawab'] = "1";
$this->db->where('id_soal', $each)->update('soal', $data);
} // 'soal' is my table name
} // 'id_soal' is my column name AS id
} // 'jawab' is my column name AS answer
}
这是我的观看代码:
<form action="<?php echo $form_action; ?>" method="post">
<?php echo $link; ?> <!-- <-- for pagination link -->
<!-- HERE WE ARE -->
<?php foreach($query->result() as $row) { ?>
<span> <?php echo $row->id_soal . '. ' . $row->soal; ?></span>
<input type="checkbox" checked="checked" value="1<?=$row->id_soal?>" name="jawab[]" />
<input align='center' type='submit' value='SELESAI' />
这是我的数据库架构:
CREATE TABLE `soal` (
`id_soal` int(10) NOT NULL AUTO_INCREMENT,
`id_bagian` int(10) DEFAULT NULL,
`soal` varchar(256) DEFAULT NULL,
`jawab` int(5) DEFAULT NULL,
PRIMARY KEY (`id_soal`)
) ENGINE=InnoDB AUTO_INCREMENT=232 DEFAULT CHARSET=latin1