-1

Ok so I went through some of the questions similar to my question but i did not get what to do with mine. I have a function in my model to get single restaurant from db, and i have a field that holds all the opening days of the particular restaurant as comma separated value, (1,2,3) 1 = Sunday 2 = Monday 3 = Tuesday and so on up to 7...

DB Table

and i want to check the checkbox when the restaurant has opening days like if restaurant one is having 1,2,3 it should check Sunday, Monday and Tuesday from front-end view.. Here is my model... Sorry for so long question... Pls help to how to do it...

function get_single_restaurant($id){
    $this->db->from('restaurants');
    $this->db->where('id', $id);
    $query = $this->db->get();
    $output = '' ;
    foreach($query->result() as $row)
    {
        $output .= '<div class="modal-header">
        <h5 class="modal-title"><i class="fas fa-utensils"></i> View Restaurant</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="view_restaurant_name">Restaurant Name </label>
                    <input type="text" class="form-control" name="view_restaurant_name" id="view_restaurant_name" value="'.$row->restaurant_name.'" readonly>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label for="view_max_pax">Maximun No. of Pax </label>
                    <input type="number" min="0" class="form-control" name="view_max_pax" id="view_max_pax" value="'.$row->max_pax.'" readonly>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="name"><b>Opening Days </b></label>
                      <div class="row">
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_sunday" value="1" disabled>
                            <label class="custom-control-label" for="view_sunday">'.$row->opening_days.'</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_monday" value="2" disabled>
                            <label class="custom-control-label" for="view_monday">Monday</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_tuesday" value="3" disabled>
                            <label class="custom-control-label" for="view_tuesday">Tuesday</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_wednesday" value="4" disabled>
                            <label class="custom-control-label" for="view_wednesday">Wednesday</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_thursday" value="5" disabled>
                            <label class="custom-control-label" for="view_thursday">Thursday</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_friday" value="6" disabled>
                            <label class="custom-control-label" for="view_friday">Friday</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_days" name="opening_days[]" id="view_saturday" value="7" disabled>
                            <label class="custom-control-label" for="view_saturday">Saturday</label>
                          </div>
                        </div>
                      </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="name"><b>Meal Times </b><span class="text-theme-color">*</span></label>
                      <div class="row">
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_meal_times" name="view_breakfast" id="view_breakfast" value="1" disabled>
                            <label class="custom-control-label" for="view_breakfast">Breakfast</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_meal_times" name="view_lunch" id="view_lunch" value="2" disabled>
                            <label class="custom-control-label" for="view_lunch">Lunch</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_meal_times" name="view_dinner" id="view_dinner" value="3" disabled>
                            <label class="custom-control-label" for="view_dinner">Dinner</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_meal_times" name="view_evening_class" id="view_evening_class" value="4" disabled>
                            <label class="custom-control-label" for="view_evening_class">Evening Class</label>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input add_meal_times" name="view_morning_class" id="view_morning_class" value="5" disabled>
                            <label class="custom-control-label" for="view_morning_class">Morning Class</label>
                          </div>
                        </div>

                      </div>
                </div>
            </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <div class="form-group">
              <label for="description"><b>Description</b></label>
              <textarea name="view_restaurant_description" id="view_restaurant_description" class="form-control" disabled>'.$row->restaurant_description.'</textarea>
            </div>
          </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                      <label for="view_restaurant_status"><b>Restaurant Status</b></label><br>
                      <h6>'.$row->restaurant_status.'</h6>
                </div>
            </div>
        </div>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>                        
                    ';
    }

    return $output;
}

Here Is my View in front-end

Front-End View

And this is my Controller

function getRestaurant(){
    $id = $this->input->post('restaurant_id');
    $data = $this->sm->get_single_restaurant($id);
    if($this->sm->get_single_restaurant($id)){
        echo $data;
    }else{
        $data = array(
            'StatusCode' => '0',
            'message' => 'Invalid request'
        );
        echo json_encode($data);
    }
}
  • Without knowing CodeIgniter, it seems you need to map the values to a clear representation in your UI. But maybe you can try to be more clear in the question itself. – Pipetus Jun 10 '20 at 15:01

1 Answers1

0

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&nbsp; 
<!-- 
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&nbsp; <!-- add attribut data-day with its value = day number -->
<input type="checkbox" value="2" data-day="2" name="opening_days[]" id="view_monday">Monday&nbsp;
<input type="checkbox" value="3" data-day="3" name="opening_days[]" id="view_tuesday">Tuesday&nbsp;
<input type="checkbox" value="4" data-day="4" name="opening_days[]" id="view_wednesday">Wednesday&nbsp;
<input type="checkbox" value="5" data-day="5" name="opening_days[]" id="view_thursday">Thursday&nbsp;
<input type="checkbox" value="6" data-day="6" name="opening_days[]" id="view_friday">Friday&nbsp;
<input type="checkbox" value="7" data-day="7" name="opening_days[]" id="view_saturday">Saturday&nbsp;

Edit 1

Ok, so I guess, I misunderstood your question, what you want is to check the checkboxes with the values already stored in the database. You can do this like so -

<input type="checkbox" value="<?php echo $row->opening_days; ?>" id="special"><?php echo $row->opening_days; ?>

<?php 
    // make an array of the values - 
    $element = explode(",", $row->opening_days));
?>

<input type="checkbox" value="1" data-day="1" name="opening_days[]" id="view_sunday" <?php echo in_array("1", $element) ? 'checked' : ''; ?>>Sunday 
<!-- if 1(day number) is present in array element then check else unchecked -->
<input type="checkbox" value="2" data-day="2" name="opening_days[]" id="view_monday" <?php echo in_array("2", $element) ? 'checked' : ''; ?>>Monday
<input type="checkbox" value="3" data-day="3" name="opening_days[]" id="view_tuesday" <?php echo in_array("3", $element) ? 'checked' : ''; ?>>Tuesday
<input type="checkbox" value="4" data-day="4" name="opening_days[]" id="view_wednesday" <?php echo in_array("4", $element) ? 'checked' : ''; ?>>Wednesday
<input type="checkbox" value="5" data-day="5" name="opening_days[]" id="view_thursday" <?php echo in_array("5", $element) ? 'checked' : ''; ?>>Thursday
<input type="checkbox" value="6" data-day="6" name="opening_days[]" id="view_friday" <?php echo in_array("6", $element) ? 'checked' : ''; ?>>Friday
<input type="checkbox" value="7" data-day="7" name="opening_days[]" id="view_saturday" <?php echo in_array("7", $element) ? 'checked' : ''; ?>>Saturday

Edit 2

First of all, you should never write HTML on model or controller for that matter. Now to answer your question, look at the Edit 1 part of my answer, that code will work. You only need to make sure not to use PHP tags as you're already writing HTML within PHP -

$output = '' ;
foreach($query->result() as $row){

    $output .= 'bla bla bla';
    $output .= '<input type="checkbox" value="'.$row->opening_days.'" id="special">'.$row->opening_days;

    $element = explode(",", $row->opening_days));

    $output .= '<input type="checkbox" value="1" data-day="1" name="opening_days[]" id="view_sunday" '. in_array("1", $element) ? "checked" : "".' >Sunday';
    $output .= '<input type="checkbox" value="2" data-day="2" name="opening_days[]" id="view_monday" '. in_array("2", $element) ? "checked" : "".' >Monday';
    $output .= '...'; // same for rest of the days
    $output .= 'bla bla bla';

    return $output;
}

You might run into some syntax error on the code above due to misplaced {'} or {"} or {.} but I'm sure you're able enough to fix them yourself.

sauhardnc
  • 1,961
  • 2
  • 6
  • 16