Jump to content

Accessing the value of different fields inside a repeater


John W.
 Share

Recommended Posts

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?

 

Link to comment
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.

  • Like 1
Link to comment
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};
    }
}

 

  • Like 5
Link to comment
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 --

 

  • Like 1
Link to comment
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.

  • Like 3
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

×
×
  • Create New...