Rob Rob - 4 months ago 12
PHP Question

Take count of one loop, output the amount that's left

I have a set of 12 circles, some will be blank, some will have an icons (this is driven by Wordpress whether they have an icon or not).

I then have a standard Wordpress loop looking for icons, if an icon is present then it will output. It also iterates the

$counter
variable starting at 1.

How can I take the count of that - then create a new loop to create blank circles?

So for instance, if 5 circles have icons then I would need 7 blank circles.

This is my attempt, at the moment it's creating an infinite loop. So from the example this needs to output 7, with the class name outputting the numbers 7, 8, 9 etc up to 12 to fill in the blanks.

Where am I going wrong?

<?php $final = 12 - $counter;

for($count = 1; $count <= $final; $count++) { ?>
<a class="research-circle blank-circle rs-<?php echo $final; ?>" href="#"></a>
<?php $final++; } ?>

Answer

It's infinite because you're increasing both values simultaneously.

Try this:

for($count = 1; $count <= $final; $count++) { ?>
    <a class="research-circle blank-circle rs-<?php echo ($final + $count); ?>" href="#">     
</a>                          
<?php } 
?>