好吧,我经历了一些与我的问题类似的问题,但我没有得到什么做我的.我有一个函数在我的模型中得到单一的餐厅从db,我有一个字段,持有所有的开放天的特定的餐厅作为逗号分隔的值,(1,2,3)1 =周日2 =周一3 =周二等7 ...
我想在餐厅有营业日的时候勾选复选框,比如如果餐厅一有1,2,3,它应该从前端视图中勾选周日,周一和周二...这是我的模型...。对不起,这么长的问题... 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">×</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;
}
这是我的前端视图
这是我的控制器
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);
}
}
你的问题似乎与 javascript
而非 php/codeigniter
. 然而,你需要的是做一个单独的 checkbox
并给它一个您希望被选择的所有字段的值,当 clicked
(像你这样),给它一些 id
,增加一个 event(change)
那就给 attribute
对所有 inputs
并据此进行选择。我已经创建了一个工作片段(不含 php
当然),必要的时候会提到评论。看看对你有没有帮助。
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
好吧,我想,我误解了你的问题,你想要的是检查你的 checkboxes
中已经存储的值。database
.你可以这样做 -
<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
首先,你千万不要写 HTML
关于 model
或 controller
对于这个问题。现在回答你的问题,看看 编辑1 我的答案的一部分,该代码将工作。你只需要确保不使用 PHP
标签,因为你已经在写 HTML
内 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;
}
你可能会在上面的代码中遇到一些语法错误,因为放错了位置。{'}
或 {"}
或 {.}
但我相信你有足够的能力自己解决它们。