Counting Grandchildren

I’m trying to count the grandchildren of a subpage, inside a loop, the first one outputs the correct result… but then every other has the same result.

 <?php foreach($countries as $country): ?>
 <li class="arrivals-item">
 <div class="arrivals-title">
 <h2 class="arrivals-flipbox">
 <?= $country->title()->html() ?>
 </h2>
 <p class="arrivals-caption">
 <?= $country->delivery()->html() ?>
 </p>
 </div>
 <div class="arrivals-status">
 <p class="arrivals-flipbox">
 <?php if(!$country->days()->empty()): ?>
 <?= $country->days()->html() ?> Days
 <?php else: ?>Awaiting Data
 <?php endif ?>
 </p>
 <p class="arrivals-caption">
  Based on <?= page('countries')->children($data['country'])->children()->count() ?> orders
 </p>
 </div>
 </li>

<?php endforeach ?>

Whereby this is my count

<?= page('countries')->children($data['country'])->children()->count() ?>

That doesn’t make sense, or maybe the parameter is ignored, you can’t pass a parameter to the children() method.

So what are you trying to. count here? What is the subpage, what. are the grandchildren?

<?= $count = page('countries')->children($data['country'])->Children()->count() ?>

Is counting how many children are in a sub folder of a main folder.

So for example

  • countries
    • USA
      - page
      - page

Returns a 2 count. Which is correct. However other countries with no sub pages, are still being counted as 2.

<?= $count = page('countries')->children()->find($data['country'])->children()->count() ?>

The find() is missing in your code, as I said, the children. method doesn’t accept a parameter.
I recommend you always. check in the cheat sheet if what you are doing is possible. All methods are explained with their parameters.

Hi, sorry your code also produces an error. Call to a member function children() on boolean

Then either the countriespage doesn’t exist, or the $data['country']page doesn’t exist (assuming, $data->country is a country name). Of course, before calling any. methods, you must either be sure they will exist (dangerous) or check if they exist.

<?php
$count = null;
if($country = page('countries')->children()->find($data['country'])) {
  $count = $country->children()->count();
}
2 Likes