arrays - Collect all records from database and checked the exist -
i'm working in yii framework , have 1 problem . have 1 table amenities , want find all.
$amenities = roomtypeamenity::model()->findall(); command work fine .
then want find amenities have room , type $amenities_room = roomtypeamenity::model()->with('idroomtypeamenity')->findall(); , command work fine . when print amenities want checked exist amenities .
foreach ($amenities $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 }?>
use if else
condition inside for-loop
. check whether current amenity_room exists current amenity. if exits, checked
checkbox
.
foreach ($amenities $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 }?>
hope helps!
Comments
Post a Comment