holmescreek

Accessing the value of different fields inside a repeater

7 posts in this topic

I've been trying to figure a way to output the values of several fields within a single repeater using a foreach() without having to reference the name of the fields.

Typically, for repeaters, I understand you would do foreach($repeater_field as $afield) { echo $afield->fieldname_a; } pretty straight forward.

Anyways, what I'm doing is using a single repeater as just a container for other fields.

I've tried using $page->my_repeater->getArray(), then looping through the array, and all sorts of methods, without luck. It seems I must specify the field name for output.

For instance, my (single) repeater is set up to hold a few different fields:

my_repeater

  • field_a
  • field_b
  • field_c

Trying to output it using something like this:

foreach($page->my_repeater as $afield) {

     echo $afield->value;   // I'm not wanting to have the reference the field name e.g. use $afield->field_a

}

 

Any suggestions?

 

Share this post


Link to post
Share on other sites

Something like this maybe?

<?php

// get all field names
$fieldNames = $page->repeater_field->first->fields->explode('name');
// get all content from all fields
$content = $page->repeater_field->explode($fieldNames);

// array comes out multidimensional unfortunately

// iterate recursively
// https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array
$iter = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($content));
// and print all content
foreach($iter as $part) {
    echo $part;
}

This works in my setup.

1 person likes this

Share this post


Link to post
Share on other sites

I think perhaps I am not understanding what you are looking for, but does this suit your needs?

foreach($page->my_repeater as $item) {
    foreach($item->fields as $field) {
        echo $item->{$field->name};
    }
}

 

4 people like this

Share this post


Link to post
Share on other sites
3 hours ago, adrian said:

I think perhaps I am not understanding what you are looking for, but does this suit your needs?


foreach($page->my_repeater as $item) {
    foreach($item->fields as $field) {
        echo $item->{$field->name};
    }
}

 

 

Exactly what I was looking for. That worked  @adrian

You provided a great solution that works.

I would have never thought of $item->{$field->name} as storing the value of the field.

The way I was originally thinking, in pseudocode was something like:

$all-repeater-fields = $page->my_repeater->getchildren();

foreach($all-repeater-fields as $a-field) {

echo $a-field->name;

echo $a-field->value;

}

Could you also guide me into a direction in the PW docs or method in which you came across this?  It seems little things like this, I dig into the documentation and API reference, and just can't find good examples/explanations for these types of questions. I try using var_dump() and the docs to try to 'connect' the dots, but something with the way I'm researching just isn't 'clicking'. The idea of $item->{$field->name} holding the value for the field would have never crossed my mind.

Thanks so much for the assistance --

 

1 person likes this

Share this post


Link to post
Share on other sites

Glad it worked for you :)

3 hours ago, holmescreek said:

The idea of $item->{$field->name} holding the value for the field would have never crossed my mind.

Remember that $field->name is going to refer to things like "body", "headline", etc so it's actually calling: $item->body which returns the value of the body field. 

The use of $item->fields is like $page->fields which is mentioned in the docs and cheatsheet.

Shameless plug, but I really find the Console panel in Tracy Debugger an incredibly useful tool - it lets you try code and dump objects and arrays in a nicely formatted way - it really helps the learning process.

3 people like this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Sipho
      Is there a module or plugin that is like a repeater but you can choose a template? I want something that let's me:
      choose a template to get fields from, display these fields like a repeater does add new pages without adding a title or name (this might be achievable using other means like ProcessSetupPageName)  delete pages I feel like somebody might have already made a module that does just this, however, I haven't found any. Perhaps there is a way of doing this with a standard repeater that I don't know about.
    • By jothanne
      I have a problem realizing a picture gallery with albums using repeater fields.
      Here is what I have:
      - The repeater field 'repeater_gallery' for all the albums
      - A text field 'album_name' in the repeater for the album name
      - An image field 'album_images' in the repeater for all the album images
      Now I'd like to pick a small selection of images (say 4) of ALL albums randomly and present them with their associated album name.
      How can this be done with the described repeater setup? Or is there a better way to realize such a gallery without repeaters?
      Thanks in advance for a little help.
    • By creativejay
      My brain is probably just tiring out on me right this moment, I'm hoping that by the time I write out my problem I'll see the way through it. If you're reading this, it didn't work.
      Structure in question is:
      Series Page Product pages Page Fields for each product page Some of these fields are repeaters Fields within the repeater I have made an array of page fields so I don't have to keep track of them all as I develop:
      $listings = $page->children; // grab all the published children of the Series page foreach($listings as $l) { // loop through the children foreach($l->fields as $f) { } // loop through each child's page fields where they have a value set } Then I break down how to handle each type of field:
      if($f->type == 'FieldtypeFile') { } elseif($f->type == 'FieldtypeDatetime'||$f->name == 'prod_status_pages'||$f->type == 'FieldtypeImage'){ } elseif($f->type == 'FieldtypePage'){ } These are all largely working as expected (though I do have a couple of offset/isset exceptions to clean up...)
      It's when I get to the repeaters that I run into trouble getting the API calls to work.
      Just cycling through the fields as above, the output for a FieldtypeRepeater is the ID of the repeater in that field's array. Everything I read suggests I should treat a repeater the same as I would treat a page, which leads me to the following code.
      elseif($f->type == 'FieldtypeRepeater'){ // Repeaters need special treatment otherwise output is just ID $th .= "<th><b>{$f->label}</b></th>\n"; $trows = ""; // creating an empty variable to build my foreach into foreach($f->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID $trows .= "{$rf->label}: {$f->get($rf)->title} ({$rf->type})<br />\n"; // add an entry to the variable } $rows .= "<td style='padding: 8px 16px; vertical-align: middle;'>".$trows."<br />\n ({$f->type})</td>\n"; // back out to rendering the Repeater field }  
      What I would hope would output in the HTML I've been building is something like:
       
      <td style='...'>Lo temp: -40 (Integer)<br /> Hi temp: 75 (Integer)<br /> Storage Lo: -40 (Integer)<br /> Storage Hi: 85 (Integer)<br /> Functional to: 85 (Integer)<br /> (FieldtypeRepeater)</td> So what I'm trying to do here is loop through the populated fields in the unknown Repeater field, and output them as a simple (so far) text list of the repeater.field and its value (and then its type for my reference).
      I'm afraid typing this out has fixed some syntax but not enough to get this working as I'd hoped.
      Please note not all Repeater fields are integers. Some also have floats, files, or options, and probably a couple others I'm forgetting.
       
      I appreciate your time in taking a look at this!
    • By nasenfloete
      Hey awesome PW-Community,
      I've some strange behaviour in my latest project and I don't know how to get on top of it:
      I added a repeater field to the User-template. In my project I create a new user than assign items to the repeater.  This works perfect, if the new user has the role "superuser" but if I create a new role (even if assign that role all permissions possible), I can't add items to the repeater via the api.
      I even checked the permissions of the repeater and assigned all permissions to the newly created role. There are also no error logs, even though it seems like the creation of a new repeater element fails.
      Thanks in advance for your help!
       
    • By benbyf
      Done quite a bit of Googling and haven't quite worked out how one would create a field of type repeater and add fields to it via the API in a module. Anyone have any links or examples?