0

i need to show checked values in checkbox from a db column (named "course")

my controller

public function CheckBoxUpdate($id=0)
{
    $data['course_taken']=$this->StudentModel->getInvoice($id);
    $this->load->view('site' ,$data);
}

My model

public function getInvoice($id=0)
{
    return $this->db->select('*')
            ->from('checkbox')
            ->where('id', $id)
            ->get()->row_array();
}

My view

<?php 

  $course=explode(",",$course_taken['course']);

  print_r ($course);

?>

<input class="form-check-input" type="checkbox" id="inlineCheckbox1" name="course[]" value="<?php echo $course_taken['course'];?>"   >
<label class="form-check-label" for="inlineCheckbox1"><?php echo $course_taken['course'];?></label>

My database

id       course
===     ========
1       option1,option2

How can i display these to selected item in checkbox as checked in codeigniter

0

3 Answers 3

1

I assume you have a set of the whole checkboxes named $checkboxes like this :

<?php
$checkboxes = array(
    '0' => 'option0', 
    '1' => 'option1', 
    '2' => 'option2', 
    '3' => 'option3', 
    '4' => 'option4', 
    '5' => 'option5', 
);
?>

Looking at your controller, I assume you have all the checked item as $course_taken.
You could display both the checked and unchecked item like this :

<?php
$course = explode(",",$course_taken['course']);
foreach ($checkboxes as $key => $value) {
?>
    <input class="form-check-input" type="checkbox" id="inlineCheckbox<?php echo $key ?>" name="course[]" value="<?php echo $value ?>" <?php echo in_array($value, $course)?'checked="checked"':'' ?> >
    <label class="form-check-label" for="inlineCheckbox<?php echo $key ?>"><?php echo $value ?></label>
<?php
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Here's i can do for you.. try it.

$courses=array('option1','option2'); //type manually all your course option
$purposesFromDB= $pr['purpose'];
$coursesFromDBArray=explode(',',$coursesFromDB); //explode data from database 
   foreach($purposesFull as $opt){
   $checked = in_array($opt,$purposesFromDBArray) ? 'checked' : ''; //check if it's in the array
   echo '<input type="checkbox" name="options[]"'.$checked.'>'.$opt;

2 Comments

i need to call options dynamically, bcoz these options are coming from another db table
call the options from another table. make sure the data is in array form then store that to variable $courses.
0

Do like this

$course=explode(",",$course_taken['course']);

<?php if(count($course) > 0): 
      foreach($course as $row):?>

    <input class="form-check-input" type="checkbox" id="inlineCheckbox1" name="course[]" value="<?= $row ?>"  <?php if($row == 'course-name'){ echo 'checked="checked"';}?> >
    <label class="form-check-label" for="inlineCheckbox1"><?= $row ?></label>

<?php endforeach; endif; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.