Jump to content

Recommended Posts

Posted (edited)

Hi, I have just set up a basic repeater. It has a title field, a body field, and an image field. I can get the title and body to show up, but I don't know what code to use for an image.  I have checked many threads about Repeater, but they are above my php ability level to make sense of.

This is what I have so far: 

    foreach($page->myrepeat as $myrepeat) {
    echo "<p>{$myrepeat->title}</p>";
    echo "<p>{$myrepeat->body}</p> ";  
    }

So far so good... but how to get the image field to echo too? I copied some working code I was using for a different image field, and tried to use it here, but nothing:

     <?php 
        foreach($page->myrepeat as $image) {
        echo "<img src='$image->url'>";
    }?>

I have given the image field its own unique name (myphoto) but I don't know how to work that in. All I did was add it to the code above, I'm sure it's not the proper syntax:

 <?php 
        foreach($page->$myrepeat->myphoto as $image) {
        echo "<img src='$image->url'>";
    }?>

Any ideas on how to get this repeater image to show up? Thanks!

Edited by ryanC
issue was solved
Posted

Hi psy, when I check my images field it's set to 0, no limit. I don't know if there is more than one place to change this setting. 

Posted
5 minutes ago, ryanC said:

Hi psy, when I check my images field it's set to 0, no limit. I don't know if there is more than one place to change this setting. 

Unless set to "1", image fields return an array (as do File fields, and many others). You have to either foreach through all images, all call a specific one. You can do: first(), last(), or eq(n). 

Read more about it here: https://processwire.com/api/fieldtypes/images/

  • Like 2
Posted

Thanks Adrian, I am able to get all the other images to show up, using the example from that link. Just not for the repeater field.

How would I alter this code to echo an image in a repeater field? This is working code I already have for a separate field.

    <?php 
        foreach($page->myrepeat as $image) {
        echo "<img src='$image->url'>";
        }
    ?>

 I am not at the PHP level a lot of people here are at. 

 

Posted

Repeaters are another one of those fields that returns an array so you need to foreach through each of the repeater items, then grab the images from that repeater item. On mobile so no code example but hope that makes sense. 

Posted

There is  slight mistake in what @PWaddict wrote - you still need to access that "first" image in the array

I would also advise using more meaningful variable names, eg use $repeater_item rather than $image in the loop, because it's not actually the image that you are getting, but rather the repeater item that has title, body, and image field in it.

foreach($page->your_repeater_field as $repeater_item) { 
	echo "<img src='$repeater_item->your_image_field->first->url'>"; 
}

The other option is this if you want to output all images from your image field.

foreach($page->your_repeater_field as $repeater_item) { 
	foreach($repeater_item->your_image_field as $image) {
	    echo "<img src='$image->url'>"; 
	}
}

 

  • Like 3
Posted
4 hours ago, adrian said:

There is  slight mistake in what @PWaddict wrote - you still need to access that "first" image in the array

Sorry for my mistake. I'm sure with your example op will understand much better how to do it.

  • Like 2
Posted

Thanks for your help, PWaddict and Adrian. I am going over your examples and I guess I'm not seeing it yet. I have named the main repeater field "myrepeat" and the image field within it "myphoto". So going over your examples I have something like:

    <?php
    foreach($page->myrepeat as $image) { 
	echo "<img src='$image->myphoto->first->url'>"; 
    }
    ?>

also this for the second example:

    foreach($page->myrepeat as $image) { 
        foreach($image->myphoto as $image) {
        echo "<img src='$image->url'>"; 
	}
    }
   foreach($page->myrepeat as $myphoto) { 
	foreach($myphoto->myphoto as $image) {
	    echo "<img src='$image->url'>"; 
	}
    }

 

 

 

Am I getting close? 

Posted

In a website I'm building where I have CroppableImage3 field (Maximum files allowed = 1 and Formatted value = Automatic) inside a repeater here is how I'm displaying the image (thumbnail).

<?php 
foreach($page->team_members_repeater as $team_member) {
$photo = $team_member->team_member_photo->getCrop("thumbnail");
?>

<img width="<?= $photo->width ?>" height="<?= $photo->height ?>" src="<?= $photo->url . "?v=" . $photo->mtime ?>" alt="<?= $team_member->team_member_name ?>">

<?php } ?>

 

  • Like 1
Posted
1 hour ago, ryanC said:

Am I getting close?

Yeah, those should all work just fine. If they're not, then something must be odd with your settings somewhere.

Is this online somewhere I could take a look?

  • Like 1
Posted

Thanks PWaddict, I'm not sure where to start with that. I know these are basic questions but I do not know php well enough to make sense of a lot of this stuff.

 

 

Posted
1 hour ago, ryanC said:

Thanks for your help, PWaddict and Adrian. I am going over your examples and I guess I'm not seeing it yet. I have named the main repeater field "myrepeat" and the image field within it "myphoto". So going over your examples I have something like:


    <?php
    foreach($page->myrepeat as $image) { 
	echo "<img src='$image->myphoto->first->url'>"; 
    }
    ?>

also this for the second example:


    foreach($page->myrepeat as $image) { 
        foreach($image->myphoto as $image) {
        echo "<img src='$image->url'>"; 
	}
    }

   foreach($page->myrepeat as $myphoto) { 
	foreach($myphoto->myphoto as $image) {
	    echo "<img src='$image->url'>"; 
	}
    }

 

 

 

Am I getting close? 

Maybe you're accidentally hiding the image with CSS?

  • Like 1
Posted
3 minutes ago, adrian said:

Yeah, those should all work just fine. If they're not, then something must be odd with your settings somewhere.

Is this online somewhere I could take a look?

Testing on a local machine, I took some screenshots of what I have though, the repeater field itself, the field being used on a page, and my image settings:

1.png

2.png

3.png

Posted
1 minute ago, PWaddict said:

Maybe you're accidentally hiding the image with CSS?

I hope not, I can get images to show up on the page that are not in a repeater. 

This code for a different field is on the same template and it's working on the same page I'm trying to use the repeater on.

 <?php 
        foreach($page->result_pic as $image) {
        echo "<img src='$image->url'>";
    }?>

 

Posted

Your field name is "images" - it's the label that you've set to "myphoto".

Just replace the call to "myphoto" with "images" and you should be fine!

  • Like 1
Posted

 Perfect! Thanks guys! This now works:

   <?php    
       foreach($page->myrepeat as $images) { 
	   foreach($images->images as $image) {
	    echo "<img src='$image->url'>"; 
	}
    }
    ?>

 

So the label I created for "myphoto" doesn't actually do anything? Meaning, the "images" field is always going to be called images, and you can't/shouldn't rename it?

Posted
10 minutes ago, ryanC said:

 Perfect! Thanks guys! This now works:


   <?php    
       foreach($page->myrepeat as $images) { 
	   foreach($images->images as $image) {
	    echo "<img src='$image->url'>"; 
	}
    }
    ?>

 

So the label I created for "myphoto" doesn't actually do anything? Meaning, the "images" field is always going to be called images, and you can't/shouldn't rename it?

The label is what the user sees when they are editing the page in the admin, so it's very useful. You can rename the field from "images" to "myphoto", but you need to change the name of the field, not the label.

  • Like 1
Posted
3 minutes ago, adrian said:

The label is what the user sees when they are editing the page in the admin, so it's very useful. You can rename the field from "images" to "myphoto", but you need to change the name of the field, not the label.

Ok, that is where I was getting confused. I thought there was only one standard image field available, but I can create many image fields with their own names. Thanks again.

Posted
7 minutes ago, ryanC said:

Ok, that is where I was getting confused. I thought there was only one standard image field available, but I can create many image fields with their own names. Thanks again.

Yes you can, but generally there is no need to have more than one unless they are on the same template. You can use template context overrides - click on a field name in the template view (showing all fields) and set the label and other details there and these will only be for that template.

  • Like 1
Posted
4 minutes ago, adrian said:

Yes you can, but generally there is no need to have more than one unless they are on the same template. You can use template context overrides - click on a field name in the template view (showing all fields) and set the label and other details there and these will only be for that template.

Makes sense, I don't need to overcomplicate things for myself. I went back and created a new image field called "myphoto" and it works. I won't use multiple named image fields unless necessary, but at least now I understand how it works!

  • Like 1

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
×
×
  • Create New...