Divide by Zero error when working with structure

Hi,
I have 3 structures, which I am trying to count the overall number of entries in, and then the number of entries with a specific key/value pair. Then I would like to work out a percentage using these numbers.
I am getting a Division by zero error in the Kirby Debugger and can’t work out why at all.
This is the code on my page:

    $section1 = $blockName . 'LeadershipAndCommunicationKeyTasks';
    $section2 = $blockName . 'CurriculumKeyTasks';
    $section3 = $blockName . 'TimetableAndPlanningKeyTasks';

    $one = $kirby->user()->$section1()->yaml();
    $two = $kirby->user()->$section2()->yaml();
    $three = $kirby->user()->$section3()->yaml();

    $allProgress = array_merge($one, $two, $three);

    $complete = 0;
    $pending = 0;
    $incomplete = 0;
    $overall = count($allProgress);

     foreach ($allProgress as $oneProgress):
       if ($oneProgress['progress'] == 'complete') {
         $complete++;
       }
       elseif ($oneProgress['progress'] == 'pending') {
         $pending++;
        }
        else {
          $incomplete++;
        }
        endforeach;

        $percentageComplete = ($complete / $overall) * 100;

        echo $percentageComplete;

The error is on the line $percentageComplete = ($complete / $overall) * 100;. If I echo $overall I get 9 (which is correct), likewise $complete is 2, which is right.

I have no idea what to try next. If I hard-code the 9 everything works fine. Can anyone shed any light?

Also if there is a better method for what I am doing when using Kirby I would be happy to learn

Well are doing your calculation within the foreach loop, so as long as $complete is 0, you will get the error.

Hi,
Sorry I don’t understand your reply - $complete is set to 0 before the loop, and is then incremented every time the loop finds a progress field with ‘complete’ as the value. In this case it is ending up as 2.

Even if $complete is 0 after the loop, I think it should still work but have an output of 0

Sorry, that was nonsense.

Apologies, I will try and do better.

I have 3 structures, with the same set of fields in each. I need to work out how many entries there are in total across all 3, and of those how many have the field progress set to complete, and then express that as a percentage. These are the three structures:

One

  autoid: cumla0or
  progress: complete
- 
  autoid: px1agxwc
  progress: complete
- 
  autoid: 8fs8d7ps
  progress: pending
- 
  autoid: m5z0lxna
  progress: pending

Two

  autoid: k46l0zaw
  progress: incomplete
- 
  autoid: kykg3dk4
  progress: incomplete

Three

  autoid: vatgjyib
  progress: pending
- 
  autoid: xnlmswue
  progress: incomplete
- 
  autoid: bb04psb8
  progress: pending

Above was my attempt to achieve this, by merging the arrays together in PHP and then looping through them and checking the value of the progress field, and incrementing some variables based on that value.

I don’t know why your code doesn’t work.

But anyway, here’s how I’d do it:

$one   = $page->field1()->yaml();
$two   = $page->field2()->yaml();
$three = $page->field3()->yaml();
$all   = array_merge($one, $two, $three);
$count = count($all);

$complete = array_filter($all, function($item) {
  return $item['progress'] === 'complete';
});
dump(count($complete) / $count * 100);