我在Yii框架中工作,我有一个问题。 我有一张带有便利设施的桌子,我想找到它。
$ amenities = RoomTypeAmenity :: model() - > findAll(); 这个命令工作正常。
然后我想找到这个房间和类型的设施 $ amenities_room = RoomTypeAmenity :: model() - > with('idRoomTypeAmenity') - > findAll(); 这个命令工作正常。但是当打印设施时,我想检查现有的设施。
foreach ($amenities as $amenity) { ?>
<input type="checkbox" class="css-checkbox" id="facility_<?php echo $amenity->id_room_type_amenity; ?>">
<label class="css-label" for="facility_<?php echo $amenity->id_room_type_amenity; ?>"><?php echo $amenity->name; ?></label>
<?php }?>
答案 0 :(得分:1)
在if else
内使用for-loop
条件。检查当前的舒适性是否存在当前的舒适性。如果退出,则checked
checkbox
。
foreach ($amenities as $amenity) { ?>
if($amenity->id_room_type_amenity!=null){
<input type="checkbox" class="css-checkbox" id="facility_<?php echo $amenity->id_room_type_amenity; ?>" checked>
<label class="css-label" for="facility_<?php echo $amenity->id_room_type_amenity; ?>"><?php echo $amenity->name; ?></label>
} else {
<input type="checkbox" class="css-checkbox" id="facility_<?php echo $amenity->id_room_type_amenity; ?>">
<label class="css-label" for="facility_<?php echo $amenity->id_room_type_amenity; ?>"><?php echo $amenity->name; ?></label>
}
<?php }?>
希望这有帮助!