[Solved] Count variable outside of container if/else

Hi all

Is there a way of getting the value of $items before I run the foreach loop so I can use the code below? Maybe a better solution?

<?php if ($items === 1): ?>
    <div class="field-items">
      <?php foreach ($items as $item): ?>
        <div class="field-item"><?php print render($item); ?></div>
      <?php endforeach; ?>
<?php else: ?>
    <div class="field-items carousel">
      <?php foreach ($items as $item): ?>
        <div class="field-item"><?php print render($item); ?></div>
      <?php endforeach; ?>
<?php endif; ?>

Any suggestions?

Thanks, Barry

Standard Programming Theorum: If you’re typing the same thing more than once, you’ve missed a compression.

The above can be reduced to:

<div class="field-items<?php (count($items) ===1) ? '' : ' carousel';  ?>">
  <?php foreach ($items as $item): ?>
    <div class="field-item"><?php print render($item); ?></div>
  <?php endforeach; ?>

EDIT: Got my if backwards.

I’ll make note, thanks :slight_smile:

Ok, something not right, I like your code here reduces everything, though what I think is happening is that I can only get the value of $items once it has run through the foreach. This is the problem I was having.

Basically, $items has no value until we run the foreach, or maybe I have things mixed up.

What do you suggest?
Carousel is not added, no matter how many items I have.

Thanks, Barry

‘the value of items’.

As presented, $items is an array that must exist and have at least one element before you start the foreach (otherwise, the foreach would do nothing). What ‘value’ are you attempting to retrieve? An array does not intrinsically have “a” value.

Ok thanks.

I want to count how many item in items.
If more than 1, then add the carousel, else add nothing.

Maybe we should be counting the $item instead?

My original code if this helps:

<div class="field-items carousel">
  <?php foreach ($items as $item): ?>
    <div class="field-item"><?php print render($item); ?></div>
  <?php endforeach; ?>
</div>

Thanks, Barry

However, my code forgot to put ‘echo’ in there.

<?php **echo** (count($items) ===1) ? '' : ' carousel'; ?>

Ok, :sunglasses: Easily done, working great now, thanks StarLion.

Ok, couple of tweaks… if you can confirm.

  • If 0 or 1 then add nothing ( we simple change === to <= )
  • If 1 add .single ( === )
  • If more than 1 add .carousel ( > )

Can we have another elseif inside this short hand way?

Barry

You can nest ternaries, it gets a little uglier, and generally repetition of function tells you to pull the function call out…

<?php $count = count($items); //By definition, $count can only be 0 or greater. ?>
<div class="field-items<?php ($count === 0) ? '' : (($count === 1) ? ' single' : ' carousel'); ?>">

(You cannot add ‘nothing’ (represented as the empty string ‘’) if 0 or 1 and then add single if 1. 1 cannot do both in a single ternary. It’s the same as if…elseif. If the first condition was true, doesnt matter if any subsequent conditions are true, the first set gets done and then jump over the rest.)

Manual Reference: Comparison Operators (section marked Ternary Operator)

(You cannot add ‘nothing’ (represented as the empty string ‘’)

Thats what I meant ’ ’ as we already have.
I was checking the Ternary operators before, and internet went down, hence my late reply.

By definition, $count can only be 0 or greater.

Another good statement.

I think thats what I was misunderstanding. Yes your right.
I’ll have another go at this and get back shortly with the outcome.

Cheers, Barry

This is perfect StarLion :sunglasses:
Everything working good, some good pointers thank you!

Barry

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.