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 Zicky - March 27, 2016 - edited: March 27, 2016

Anyone have any suggestions on how to limit the total upload records shown inside a foreach loop? Here's my code which I've bolded some text showing what I'm trying to do.

// load records from 'services'
  list($servicesImgPanelsRecords, $servicesImgPanelsMetaData) = getRecords(array(
    'tableName'   => 'services',
    'limit'       => '3',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

I tried limit => '3' as seen above, but it only displays on two records, I guess because I have 4 records and one does not have an image... so that wasn't working for me.

So I think it needs to be filtered in the foreach loop... so records without uploads are not included, and there's a total limit of 3 records that will display with uploads.

<?php foreach ($servicesImgPanelsRecords as $record): ?>
                 <?php foreach ($record['uploads'] as $index => $upload): ?>
                    
                    Maximum Uploads Displayed = 3
                    
                    <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 ?>

Thanks Zicky

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 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/