Display images by upload info1 record

4 posts by 2 authors in: Forums > CMS Builder
Last Post: February 12, 2019   (RSS)

By degreesnorth - February 7, 2019

Hi

Is there a way to only display upload images by the info1 field?  And them limit the it to only 8 images randomly?

I have created a single record, uploaded the images, and for the info1 field (ie, title), they can be either be called "winter" or "summer".  On some pages, I only want to display the summer photos, and on other pages the winter photos.  Is there a way to sort using just this field?

BTW, I didn't use a multi record as on most of the pages, I want to display ALL of the images in random order, and it's faster to upload images in a single record.

Thanks in advance.

Attachments:

help.jpg 31K

By daniel - February 12, 2019

Hi degreesnorth,

Yes, this sort of thing is definitely possible.

First, to only display images with a specific value in the info1 field, you need to add some code to the beginning of the image loop. It would be something like this:

<?php foreach ($exampleRecord['images'] as $index => $upload): ?>

  <?php if ($upload['info1'] != 'summer') { continue; } ?>

  ...

<?php endforeach; ?>

The above will skip all records that don't have "summer" in info1. (You'll need to update the variable names to match your code)

Then, to pick only 8 images at random, we can shuffle the image array, and use a counter to make sure we only display 8 images:

<?php shuffle($exampleRecord['images']); ?>
<?php $imageCount = 0; ?>
<?php foreach ($exampleRecord['images'] as $index => $upload): ?>

  <?php if ($upload['info1'] != 'summer') { continue; } ?>
  <?php if ($imageCount++ >= 8) { break; } ?>

  ...

<?php endforeach; ?>

Let me know if that helps!

Thanks,

Daniel
Technical Lead
interactivetools.com

By degreesnorth - February 12, 2019

Hi Daniel

That works perfectly, thank you heaps!

Cheers

Carole