limit the total upload records shown inside a foreach loop

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 28, 2016   (RSS)

By Damon - March 28, 2016 - edited: March 28, 2016

Hi Zicky,

I added a counter and if statement to your code so that when three images have been displayed, the image foreachloop is exited to only show maximum three images.

<?php foreach ($servicesImgPanelsRecords as $record): ?>
                 <?php $counter = 0; ?>
                 <?php foreach ($record['uploads'] as $index => $upload): ?>
                 <?php $counter++; //increase counter number
                       if($counter > 3) {break; } 
                 ?> 
                    
                    <div class="grid_4 wow fadeInLeft" data-wow-delay=".2s">
                        <a class="lazy-img thumb" style="padding-bottom: 67.56756756756757%" href="<?php echo htmlencode($upload['urlPath']) ?>">
                            <img data-src="<?php echo htmlencode($upload['thumbUrlPath4']) ?>" src="#<?php echo htmlencode($record['num']) ?>" alt="<?php if ($upload['info1']): ?><?php echo htmlencode($upload['info1']) ?><?php else: ?><?php echo htmlencode($record['name']) ?><?php endif ?>" />
                            <noscript><img src="<?php echo htmlencode($upload['thumbUrlPath4']) ?>" alt="<?php if ($upload['info1']): ?><?php echo htmlencode($upload['info1']) ?><?php else: ?><?php echo htmlencode($record['name']) ?><?php endif ?>" /></noscript>
                            <div data-type="paw-spinner"></div>
                            <div class="thumb_overlay"><div class="fa fa-expand"></div></div>
                        </a>
                        <h3>
                        <?php if ($upload['info1']): ?>
                        <span><a href="<?php echo $record['_link'] ?>" title="<?php echo $record['service_category:label'] ?>"><?php echo htmlencode($upload['info1']) ?></a></span>
                        <a href="<?php echo $record['_link'] ?>" title="<?php echo $record['service_category:label'] ?>"><?php echo htmlencode($upload['info2']) ?></a>
<?php else: ?>
                        <a href="<?php echo $record['_link'] ?>" title="<?php echo $record['service_category:label'] ?>"><?php echo htmlencode($record['name']) ?></a>
                        <?php endif ?>
                        </h3>
                        <p><?php echo htmlencode($record['summary']) ?></p>
                        <a data-type="paw-ink" href="<?php echo $record['_link'] ?>" title="<?php echo $record['service_category:label'] ?>" class="btn1"><span class="fa fa-angle-right"></span></a>
                    </div>
                    <?php endforeach ?>
                <?php endforeach ?>

Try this out and let me know if it works for you.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Zicky - March 28, 2016 - edited: March 28, 2016

Hey Damon,

I changed exit; to break; because the rest of the page under the uploads was killed on exit loop. I really appreciate the help - it's working like a charm.

<?php $counter++; //increase counter number
      if($counter > 3) {break; } 
?> 

Zicky

By Damon - March 28, 2016

Whoops. You are right, I should have used break. Thanks for correcting that for anyone else referring to this post in the future.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/