Forum

Thread tagged as: Question

How do I display the number of items in a collection

In runway I have a collection of hotels, one of the fields in the hotel collection is 'rooms' which is the number of rooms available in a hotel.

I want to do two things: 1. Display the total number of hotels in the collection. 2. Display the total number of rooms, across the collection.

As hotels are added these figures would auto update. Any pointers appreciated, thanks.

Andy Clark

Andy Clark 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

$hotels = perch_collection('Hotels', [
    'skip-template' => true;
]);

$number_of_hotels = count($hotels);
$number_of_rooms = 0;

foreach($hotels as $hotel) {
    $number_of_rooms = $number_of_rooms + (int) $hotel['rooms'];
}

echo "There are $number_of_rooms rooms in $number_of_hotels hotels.";

Thanks Drew, I'll be using that a lot over the next few days.