Jump to content

Image repeater problem


sendxt
 Share

Recommended Posts

Hi there;) 

I have problem with images repeater, i can't return him with to the cycle. 

I do it so: 

<?php 
			    	

foreach($page->img as $k) {

	$small = $k->small_img->first();
	$big = $k->big_img->first();

	echo '<div class="break">';
	echo '<a class="fancybox-thumb" id="frame-4" rel="fancybox-thumb" href="'.$big->url.'" title="">;
	echo '<img class="img-4" src="'.$small->url.'" alt=""></a>';
	echo '</div>';
	}
					?>	

foreach return me only one field from array , but I know that there are more, how me return all elements from img ? 

Link to comment
Share on other sites

Hi Sendxt,

Welcome to ProcessWire and the Forums!

I am in a bit of a hurry. See solution here..https://processwire.com/talk/topic/6243-images-in-a-repeater-field/

Key thing to remember is that behind the scenes repeaters are also pages and need to be treated as such...hence you may also need to foreach (loop) through them....Normally, this would require nested foreachs...if the images are also an array..

foreach ($page->my_repeater_field as $f) //$f is the repeater....you need to loop through it if you have multiple images inside it...

foreach ($f->images as $image)//do stuff....
Edited by kongondo
  • Like 1
Link to comment
Share on other sites

Hi Sendxt,

Welcome to ProcessWire and the Forums!

I am in a bit of a hurry. See solution here..https://processwire.com/talk/topic/6243-images-in-a-repeater-field/

Key thing to remember is that behind the scenes repeaters are also pages and need to be treated as such...hence you may also need to foreach (loop) through them....Normally, this would require nested foreachs...if the images are also an array..

foreach ($page->my_repeater_field as $f) //$f is the repeater....you need to loop through it if you have multiple images inside it...

foreach ($f->images as $image)//do stuff....

but i can't make foreach ( $f->images as $image ) // because in my repeater field -> two fields for big_img and small_img ((  Or need to do one field and put there small and big img ?

Link to comment
Share on other sites

I did so with text, was good. 

foreach($page->product_name as $field) {		
	 				
	echo '<p class="name">'.$field->sub_name.'</p>';
	 echo '<p class="service">'.$field->sub_text.'</p></td>';			
}

with picture so can not be done?

Link to comment
Share on other sites

foreach($page->img as $k) {

	foreach($k->small_img as $image) {
	echo '<div class="break">';
		echo '<a class="fancybox-thumb" id="frame-4" rel="fancybox-thumb" href="'.$image->url.'" title="">';
		echo '<img class="img-4" src="'.$image->url.'" alt=""></a>';
		echo '</div>';
							
							}
						}

work like this, but how to do with foreach two field ? 

http://prntscr.com/3rdho9

Link to comment
Share on other sites

  • 3 weeks later...

Hi,

I'm fairly new to PW myself, but I think you need to output it like this, assuming your structure is:

- page with images

-- images (repeater field)

 --- small_image (image field)
 --- big_image (image field)

-- other content (textarea)

-- (...)

$images_to_show = $page->images // herein are all image repeater instances of the current page
foreach( $images_to_show as $imgs) {
    // now we are "in" one repeater, so we need to get the repeater's fields
    foreach( $imgs as $img) {
        // do whatever you want with the fields
        echo $img->small_image->url;
        echo $img->big_image->url;
    }
}

I coded this in the browser, not tested, but I think you're missing the loop "inside" the repeater to get to the fields you want.

Link to comment
Share on other sites

uhm, If the only reason you use the "images" as a repeater field is to get different sizes of the same image, you have other options:
If you want to control the size and aspect ration of the different sizes, but allow your editor the control of the part of the image to be shown, have a look at the fieldtypeCropImage module set http://modules.processwire.com/modules/fieldtype-crop-image/.

If you just want to use different sizes/scales of the image, you can simply make them on the fly in your output; see "resizing images" here: http://processwire.com/api/fieldtypes/images/
 

Structure w/o repeater field:

- page with images

-- images (image field)

-- other content (textarea)

-- (...)

$images_to_show = $page->images
foreach( $images_to_show as $img){
    $large = $image->width(500);
    $thumb = $image->size(100, 100); 
   echo '<a href="' . $large->url . '"><img src="' . $thumb ->url . '" /></a>';
}

Tom

Link to comment
Share on other sites

  • 2 years later...

Hello,

 

I'm trying to call images in repeater to appear in a slideshow, can somebody tell me what's wrong with this code ?

I have no problem calling H1 & paragraph.

<div class="carousel-inner" role="listbox">
    <? foreach($page->SlideImagesRepeater as $SlideImagesRepeat): ?>
        <div class="item active">

            <img src="<?=$SlideImagesRepeat->Sliders->images->url?>" alt="Hero Slide">

            <div class="carousel-caption">
                <h1><?=$SlideImagesRepeat->SlideTitle ?></h1>

                <p><?=$SlideImagesRepeat->SlideParagraph ?></p>
            </div>

        </div>
         <? endforeach; ?>
        
    </div>

Thanks

 

snap.jpg

Link to comment
Share on other sites

Ok, my bad.

 

The correct codes are :

<div class="carousel-inner" role="listbox">
    <? foreach($page->SlideImagesRepeater as $SlideImagesRepeat): ?>
        <div class="item active">

            <img src="<?=$SlideImagesRepeat->SlideshowImage->first()->url?>" alt="Hero Slide">

            <div class="carousel-caption">
                <h1><?=$SlideImagesRepeat->SlideTitle ?></h1>

                <p><?=$SlideImagesRepeat->SlideParagraph ?></p>
            </div>

        </div>
         <? endforeach; ?>
        
    </div>

but there's another problem.

There are 3 slides on the homepage, the first one has <div class="item active"> while the other 2 slides don't have any <div class="item">.

Because the class active gets repeated 3 time all slides appearing at once.

How do you fix this ?

 

 

 

 

Screen Shot 2017-03-05 at 9.07.26 PM.png

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