Jump to content

If last image


sevfurneaux
 Share

Recommended Posts

Hi there,

I'm relatively new to Processwire (and PHP!) and have the following question:

I'm currently looping through the images field as shown below:

foreach($page->images as $image) {
				$large = $image->size(1200, 795);
				echo "{$large->url},";
			}

You'll notice that there is a comma after the {$image->url}. I would like to be able to check in the loop if it is the last image and if so, the comma is not shown. Would appreciate the help!

Cheers,

Sev

Link to comment
Share on other sites

Hi sev and welcome! :)

You could do the following and strip the last char from the string after the loop.

<?php
// if there's any images
if(count($page->images)){
$out = '';
foreach($page->images as $image) {
	$large = $image->size(1200, 795);
	$out .= "{$large->url},";
}
echo substr($out, 0, -1);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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