Getting Check Box Values as Checked from Database Codeigniter

Getting check box values as checked from database codeigniter

Sometimes the use of functions from frameworks just make it totally unnecessarily and dirtier.

<input type="checkbox" name="text[]" value="text1" <?php echo ($yourVar == 'text1' ? 'checked' : null); ?>>

How to get checked checkbox values as checked in codeigniter?

I got my solution:

   <?php
$dosage_form_list_exist = preg_replace('/\s*,\s*/', ',',
$dosage_form_list_exist); // It removes spaces after the comma only not
between the values like "Oral Solutions" etc
// $dosage_form_list_exist = str_replace(' ', '', $dosage_form_list_exist);
// It removes all the spaces from the Variable
$dosage_list = explode(',',$dosage_form_list_exist);
foreach ($dosage_form_list as $val) {
?>

<input type="checkbox" name="dosage_form_input[]" value="<?php echo
$val['dosage_form']?>" <?php echo (in_array($val['dosage_form'],$dosage_list)
? 'checked' : ''); ?>>

<?php echo $val['dosage_form'];?>

<?php
}
?>

Fetch checkbox values from DB MySQL in Codeigniter

Your problem seems to be related to javascript rather than php/codeigniter.

Nevertheless, what you need is make a separate checkbox and give it a value of all the fields you want to be selected when clicked(like you're doing), give it some id, add an event(change), then give attribute to all the inputs with their day number and select accordingly.


I've created a working snippet(without the php of course), comments are mentioned wherever necessary. See if it helps you.

document.querySelector('#special').addEventListener('change', (event) => { // add event 'change' to id='special'  let elements = event.target.value.split(','); // get the value and create an array by splitting it   let checked = false; // default false  if (event.target.checked) { // check if the checkbox is checked or unchecked    checked = true; // change if checked  }    elements.forEach((item) => { // change the property of all the checkboxes where data-day = item    document.querySelector('[data-day="'+item+'"]').checked = checked;  });})
<input type="checkbox" value="1,2,3" id="special">1,2,3  <!-- Give an id='special'  and value='1,2,3' for Sunday, Monday, Tuesday <input type="checkbox" class="custom-control-input add_days" id='special' name="opening_days[]" id="view_sunday" value="'.$row->opening_days.'"><label class="custom-control-label" for="view_sunday">'.$row->opening_days.'</label>--><input type="checkbox" value="1" data-day="1" name="opening_days[]" id="view_monday">Sunday  <!-- add attribut data-day with its value = day number --><input type="checkbox" value="2" data-day="2" name="opening_days[]" id="view_monday">Monday <input type="checkbox" value="3" data-day="3" name="opening_days[]" id="view_tuesday">Tuesday <input type="checkbox" value="4" data-day="4" name="opening_days[]" id="view_wednesday">Wednesday <input type="checkbox" value="5" data-day="5" name="opening_days[]" id="view_thursday">Thursday <input type="checkbox" value="6" data-day="6" name="opening_days[]" id="view_friday">Friday <input type="checkbox" value="7" data-day="7" name="opening_days[]" id="view_saturday">Saturday 

How to show checkboxes as checked when values are set in the database in codeigniter

Finally I found It

 <?php
$arrSelVedTask = array();
foreach ($veddingPlanTaskMappingData as $row) {
$arrSelVedTask[$row->task_id] = '';
}
?>


<div class="form-group">

<lable for="task_id" class="control-label col-sm-12">Plan Task LIST:</lable>
<div class="col-sm-10">
<div class="checkbox" style="margin-left:40px;">

<?php foreach ($allVedingTasks as $row) {

if(isset($arrSelVedTask[$row->task_id])) {
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked ><?php echo $row->task_name; ?><br><?php
}
else{
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name; ?><br><?php
}
}
?>

CodeIgniter Marking checkboxes from database

just change your code like this

 <tbody>               
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr>
<td>

<?php

if (in_array($data->id, $event_contacts)) {
?>
<input type="checkbox" name="id[]" id="id" checked value="<?php echo $data->id; ?>"/>
<?php
}else{

?>
<input type="checkbox" name="id[]" id="id" value="<?php echo $data->id; ?>"/>

<?php
}
?>

</td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>


Related Topics



Leave a reply



Submit