Jump to content

Accessing fields inside repeater inside an array of page fields.


creativejay
 Share

Recommended Posts

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

Structure in question is:

  1. Series Page
    1. Product pages
      1. Page Fields for each product page
        1. Some of these fields are repeaters
          1. 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!

Link to comment
Share on other sites

Morning (here...),

9 hours ago, creativejay said:

foreach($f->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID

As far as I can see, at this stage you are looping through the Pages of the Repeater and not the fields, yet. You need nested loops:

<?php
foreach($f as $repeater_page) {
	foreach($repeater_page->fields as $rf){ // looping, I hope, through the fields of the given Repeater ID
		//echos...
	}
}

 Warning: written in the browser and without my morning coffee! ;) 

Edited by szabesz
typo
Link to comment
Share on other sites

6 hours ago, szabesz said:

As far as I can see, at this stage you are looping through the Pages of the Repeater and not the fields, yet. You need nested loops

Thank you for taking a look!

This is still not outputting anything, unfortunately. I'm fussing about with it and will update if I catch on an answer, but if anyone else is taking a look at this I am still seeking the solution.

Thanks!

Link to comment
Share on other sites

  • 1 month later...

I think you are looking for something like this. You need to be making use of the names of the repeater fields and also referencing the repeater field relative to it's containing page.

foreach($l->{$f->name} as $repeater_item) {
    foreach($repeater_item->fields as $rf) {
        echo $repeater_item->{$rf->name};
    }
}
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...