php - insert checkbox with two value codeigniter
Get the solution ↓↓↓in my view:
<form method="post" action="<?php echo base_url(); ?>Data/Penilaian/Selected">
<table class="table datatable-responsive" id="example">
<thead>
<tr>
<th><button class="btn btn-info">Submit</button></th>
<th>No</th>
<th>Kendaraan</th>
<th>Plat Nomor</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
if(!empty($getsifat)) {
foreach ($getsifat as $kr_key => $kendaraan) { ?>
<tr>
<td>
<?php foreach ($kendaraan['sub']['count'] as $data) { ?>
<input type="checkbox" name="id_kendaraan[]" value="<?php echo $kendaraan['id_kendaraan'] ?>">
<input type="hidden" name="total[]" value="<?php echo $data['total'] ?>" > //this my problem
<?php } ?>
</td>
<td><?php echo $no ?> </td>
<td><?php echo $kendaraan['nama'] ?> </td>
<td><?php echo $kendaraan['platno'] ?></td>
<?php
foreach ($kendaraan['sub']['count'] as $data) { ?>
<!-- <input type="hidden" name="total[]" value="<?php echo $data['total'] ?>"> -->
<td><?php echo $data['total'] ?></td>
<?php } ?>
</tr>
<?php
$no++;
}
}
?>
</tbody>
</table>
</form>
in view i want to insert$id_kendaraan
and$total
value
this my controller:
function Selected(){
$id_kendaraan = $this->input->post('id_kendaraan');
$total = $this->input->post('total');
$data = array();
for ( $i = 0; $i < count( $id_kendaraan ); $i++ ) {
$data = array(
'id_kendaraan' => $id_kendaraan[$i],
'total' => $total[$i]
);
$this->PenilaianModel->TambahSelected($id_kendaraan, $data);
}
redirect('Data/Penilaian', 'refresh');
}
my model:
function TambahSelected($id_kendaraan, $data)
{
$this->db->where('id_kendaraan', $id_kendaraan);
$this->db->insert('tb_ranking', $data);
}
i want to insert $id_kendaraan and $total to my db but if i try this code: examle in view:
id kendaraan value = 1, total value = 20
id kendaraan value = 2, total value = 35
id kendaraan value = 3, total value = 44
if i checked $id_kendaraan 2 and press submit, in my databases data $id_kendaraan value = 2, $total value = 20, is not correct... in my db this data should be $id_kendaraan value = 2, $total value = 35
and if i try checked $id_kendaraan 3 and press submit, in my databases data $id_kendaraan value = 3, $total value = 20, is not correct... in my db this data should be $id_kendaraan value = 3, $total value = 44
and if i try checked $id_kendaraan 2,3 and press submit, in my databases data $id_kendaraan value = 2, $total value = 20 and $id_kendaraan value = 3, total value = 35, is not correct... in my db this data should be $id_kendaraan value = 2, $total value = 35 and $id_kendaraan value = 3, $total value = 44
thx for your help
Answer
Solution:
It is happening because hidden array will send all the data but your check box will post only selected array values. So key mismatch will happen in your server side code. To solve this you need to serve array keys uniquely.
Rearrange your front end code as like follows.
<?php
$no=1;
if(!empty($getsifat)) {
$inputid = 0;
foreach ($getsifat as $kr_key => $kendaraan) {
$attch1 = $attch2 = "";
foreach ($kendaraan['sub']['count'] as $data){
$attch1 .= '<input type="checkbox" name="id_kendaraan['.$inputid.']" value="'.$kendaraan['id_kendaraan'].'">';
$attch1 .= '<input type="hidden" name="total['.$inputid.']" value="'.$data['total'].'" >';
$attch2 .= '<td>'.$data['total'].'</td>';
$inputid++;
}
?>
<tr>
<td><?php echo $attch1 ?> </td>
<td><?php echo $no ?> </td>
<td><?php echo $kendaraan['nama'] ?> </td>
<td><?php echo $kendaraan['platno'] ?></td>
<?php echo $attch2 ?>
</tr>
<?php
$no++;
}
}
?>
Rewrite controller as like
public function Selected(){
$id_kendaraan = $this->input->post('id_kendaraan');
$total = $this->input->post('total');
$data = array();
foreach ($id_kendaraan as $key => $value) {
$data = array(
'id_kendaraan' => $value,
'total' => $total[$key]
);
$this->PenilaianModel->TambahSelected($id_kendaraan, $data);
}
redirect('Data/Penilaian', 'refresh');
}
NOTE :
You were looping same loop twice. I made it as one.
in line You are printing inside loop. I guess it will come outside loop.
no need of line
$this->db->where('id_kendaraan', $id_kendaraan)
in insert context.
Answer
Solution:
Remove this line
$this->db->where('id_kendaraan', $id_kendaraan);
To update values , you should usewhere
.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: http failure during parsing for
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.