チェックボックスの回答値からデータベースの選択した行にデータを送信する際に問題があります
ページネーションも使用しています。
これが私のコントローラーです:
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