Jump to content


Photo

If last image


  • Please log in to reply
2 replies to this topic

#1 sevfurneaux

sevfurneaux

    Newbie

  • Members
  • Pip
  • 7 posts
  • 0

Posted 27 October 2011 - 01:15 PM

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

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1739

  • LocationSH, Switzerland

Posted 27 October 2011 - 01:47 PM

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);
}

@somartist | modules created | support me, flattr my work flattr.com


#3 sevfurneaux

sevfurneaux

    Newbie

  • Members
  • Pip
  • 7 posts
  • 0

Posted 27 October 2011 - 02:09 PM

Works perfectly – Thanks Soma!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users