<?php
$repeat_days = "0,1,2";
$repeat_array = explode(",", $repeat_days);
foreach ($repeat_array as $repeat_array1): ?>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="0">SUN
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="1">MON
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="2">TUE
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="3">WED
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="4">THU
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="5">FRI
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="6">SAT
</label>
<?php endforeach; ?>
我需要在php中编辑时自动选中一个复选框。在这里我输入了一个静态值,如0,1,2。 0-> SUN,1-> MON,2-> TUE。在这种情况下,如果foreach值等于复选框值,则会自动选中该复选框。我怎么能解决这个朋友呢?
请指导我
由于
答案 0 :(得分:2)
<?php
$repeat_days = "0,1,2";
$repeat_array = explode(",", $repeat_days);
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="0" if(in_arrray('0',$repeat_array)){ echo 'checked';} >SUN
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="1" if(in_arrray('1',$repeat_array)){ echo 'checked';} >MON
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="2" if(in_arrray('2',$repeat_array)){ echo 'checked';} >TUE
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="3" if(in_arrray('3',$repeat_array)){ echo 'checked'; }>WED
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="4" if(in_arrray('4',$repeat_array)){ echo 'checked';} >THU
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="5" if(in_arrray('5',$repeat_array)){ echo 'checked';} >FRI
</label>
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="6" if(in_arrray('6',$repeat_array)){ echo 'checked';} >SAT
</label>
答案 1 :(得分:1)
$days = array("SUN","MON","TUE","WED","THU","FRI","SAT");
$checked = array(0,1,2);
foreach ($days as $key => $day) {
if (in_array($key, $checked)){
$status = "checked";
} else {
$status = "";
}
$checkbox = '
<label class="checkbox-inline">
<input type="checkbox" name="day[]" value="'.$key.'" '.$status.'>'.$day.'
</label>
';
echo $checkbox;
}
答案 2 :(得分:1)
<?php
$days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
$repeat_days = "0,1,2";
$repeat_array = explode(",", $repeat_days);
foreach($days as $index => $day) {
echo '<label class="checkbox-inline"><input type="checkbox" name="day[]" value="'.$index.'"'.(in_array($index,$repeat_array)? ' checked' :'').'>'.$day.'</label>';
}