Jump to content

Getting repeater elements by pagearray value


Jennifer S
 Share

Recommended Posts

I've created sections of my page hierarchy to track different attributes, which I then use to populate a Page(Multiple) selector. They aren't displayed on the site; they're used as a kind of categorization method.

I use the PageArray selector in many of my repeating fields. Then in the template code I ask for all the items in a particular branch of the hierarchy along with every instance of a repeating field that has been associated with that page via the PageArray selector. It looks like this:

<?php if (trim($page->person)) { 
        $cluepeople = $pages->get( "id=1344" );
        $peopletypes = $pages->find( "parent=1582, include=hidden, sort=sort" );

        foreach ( $peopletypes as $peopletype ) {
            echo "<h3>" . $peopletype->title . "</h3>";
            $peopletypeid = $peopletype->id;
            settype($peopletypeid, "string");

            foreach ( $cluepeople->person as $thisperson ) {
                $peopleselectorvalue = $thisperson->people_selector;
                settype($peopleselectorvalue, "string");
    
                if ( $peopletypeid == $peopleselectorvalue ) {
                    echo $thisperson->title;
                }
            }
        }
     } ?>
 

I'd love a code review to know if this is a clumsy way of doing this and if there is an easier way (there usually is w/PW).

Thanks,

Jenn

Link to comment
Share on other sites

This is the first time I've seen PHP's settype() function in active use. You've got me curious–is there a reason why its necessary here? I think that what you are doing looks fine. But if you are looking for a code review, my opinion is that there's a lot of extra/unnecessary code and variable assignment here. For me, that makes it harder to read and comprehend what it's doing. If you find that it helps you to read it, then I that's a good reason to do it. But if it were me, I would probably shorten it up a bit like this:

$cluepeople = $pages->get( "id=1344" );
$peopletypes = $pages->find( "parent=1582, include=hidden, sort=sort" );

foreach ( $peopletypes as $peopletype ) {
  echo "<h3>" . $peopletype->title . "</h3>";
  foreach ( $cluepeople->person as $thisperson ) {
    if ( $peopletype->id == $thisperson->people_selector ) {
      echo $thisperson->title;
     }
  }
}
or this:
$cluepeople = $pages->get( "id=1344" )->person;
$peopletypes = $pages->find( "parent=1582, include=hidden, sort=sort" );

foreach ( $peopletypes as $peopletype ) {
  echo "<h3>" . $peopletype->title . "</h3>";
  foreach($cluepeople->find("people_selector=$peopletype->id") as $person) {
    echo $person->title;
  }
}
  • Like 1
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...