Jump to content

Limiting Number of Items in Loop


coert_g

Recommended Posts

Hey guys, 

We've built a custom relationship plugin that allows us to pull through items from the gallery application to the pages application, by tying the image to a listing in the pages app.

I'm looking to output these items now, and limit them. I can output them fine using the following:

 

    {{if $relationships['gallery_images'] == NULL}}No photos available for this listing, be the first to <a href="/gallery/submit/?_new=1">submit a photo</a>.{{else}}
    {{foreach $relationships['gallery_images'] as $image}}
   <div class="containThumb">
     <div class="thumbBlock_holder">
    <img class="myImg thumbBlock" src="/uploads/{$image['image_masked_file_name']}"/>
     </div>
    </div>
    {{endforeach}}
{{endif}}


But would love some assistance in being able to limit that listing to only show 8 items. Can someone please assist with the logic in these loops to limit the loop to output only 8 results.

Thanks!

Link to comment
Share on other sites

{{if $relationships['gallery_images'] == NULL}}
No photos available for this listing, be the first to <a href="/gallery/submit/?_new=1">submit a photo</a>.
{{else}}
	{{$i = 0;}}
	{{foreach $relationships['gallery_images'] as $image}}
		<div class="containThumb">
			<div class="thumbBlock_holder">
				<img class="myImg thumbBlock" src="/uploads/{$image['image_masked_file_name']}"/>
			</div>
		</div>
		{{if ++$i == 8}}
			{{break;}}
		{{endif}}
	{{endforeach}}
{{endif}}
Link to comment
Share on other sites

4 hours ago, newbie LAC said:

but it would be better if you limit result in php. No need to get 1000000 rows if you want display 8

 

^ This. Why does the array have more than you need? If it's from a database query, for instance, the performance impacts can be significant and the best solution is to use a MySQL LIMIT clause so you only have the 8 results you want in the first place.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...