Forum

Thread tagged as: Question, Problem

Merging Two Regions

Hello,

I have been struggling with this for about a week now, so hopefully someone can help me out.

I have two regions, one is called Profile and is used on all classroom pages - a page per classroom - where regions are referenced from a Master Page template. The other region is called Faculty and is located on the faculty.php page, which lists all the faculty members, each with a link to their own classroom page.

The Profile region contains classuser, email, _page, banner fields and ID's. The Faculty region contains name, email, room, title fields and ID's.

Now I have individual classroom pages - a page per classroom, as mentioned above. Then I have a Classrooms page where I want to display a list of all the individual classroom pages. The data I need is in either the Profile or Faculty regions, but if you notice, both regions have an email ID.

How can I merge these two regions, using the email ID as the common variable, and then use the fields from both regions in a single template on the Classrooms page? Sort of like a directory of the classroom pages and their corresponding faculty.

Code for Classrooms Page:

$jmUsers = perch_content_custom(array('Profile','Faculty'), array(
     'page' => array('/classroom/*','/faculty.php'),
     'template' => '_classdisplay_all.html',
     'sort' => 'room',
     'sort-order' => 'ASC'
));

Template for Classrooms page:

<div class="rmEntry">
     <h3><a href="<perch:content id="_page" />">Classroom <perch:content id="room" /></a></h3>
     <div class="banner">
          <img src="<perch:content  id="banner" />" alt="Class Banner" />
     </div>
     <div class="detail">
          <p><perch:content id="classuser" />: <perch:content id="title" /></p>
     </div>
</div>

The output I'm getting from this code and template, displays each classroom on the Classrooms page, but only the fields from the Faculty region show up. Any ideas, suggestions...?

Thanks, Joshua

Joshua Rodriguez

Joshua Rodriguez 2 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I would turn it around. Have a region of your classrooms on the classrooms page. Use a dataselect or related field type to pull the classroom information into the other regions where you need it.

That does sound like a much better solution and I may have considered it before, but here's the problem.

The content of each Classroom page is dynamic in a sense, and any changes made on an individual page would reflect on the Classrooms page.

So if, for example, the faculty member changes the way their name is displayed (eg. Mr. Smith as opposed to John Smith), or add/change their classroom page banner, they would do this from the Profile region of their own classroom page. Those changes would then need to reflect on the Classrooms page. I was using a similar method using the following code, but only using classuser, banner, _page ID's from the `Profile' region.

perch_content_custom('Profile', array(
     'page' => '/classroom/*',
     'template' => '_classdisplay_all.html'
));

My goal was to merge the two regions somehow, using an email ID as a common value, so I could add room,title data on the Classrooms page, without having to duplicate that data in a new region on the Classrooms page.

On the individual classroom pages, I used the following to pull data from the Faculty region on the faculty.php page, where the email ID was equal to the email ID in the Profile region of each classroom page:

$rmData = perch_content_custom('Profile', array(
        'skip-template' => true,
        'raw' => true
    ));
    $userEmail = $rmData[0]['email']; 

    $userData = perch_content_custom('Faculty', array(
        'skip-template' => true,
        'page' => '/faculty.php',
        'filter' => 'email',
        'match'  => 'eq',
        'value'  => $userEmail,
    ));
    PerchSystem::set_vars(array(
        'profile-name'  => $userData[0]['name'],
        'profile-email' => $userData[0]['email'],
        'profile-region'=> $userData[0]['region'],
        'profile-title' => $userData[0]['title'],
        'profile-room'  => $userData[0]['room'],
    ));

Then I used the set_vars in a template to display the data as needed.

perch_content_custom('Profile', array(
        'template' => '_classdisplay_profile.html',
)); 

I figured I could use the same concept with the Classrooms page. The difference though is that with each classroom, the set_vars data is filtered by the email ID, so each page pulls only the faculty data that corresponds to their classroom. If only there was a way to use PerchSystem::set_vars, but without filtering for a single classroom page result, but instead an array of classroom pages. I don't know if I'm explaining myself well, but hopefully you see what I'm trying to do?

Thanks, Joshua

I got it!

I was hoping for something more simple, but I just want to let you know that I was able to figure it out. Pheww!!!

First, I gathered the data from one region:

$userData = perch_content_custom('Profile', array(
        'skip-template' => true,
        'page'   => '/classroom/*'
    ));

Then I used a foreach loop to pull the values I needed for each classroom page, placed the email value in a variable to reference in both regions, used PerchSystem to pass the variables into my template, and finally created a template for the other region, passing in the variables I had set, and filtering by my email variable:

foreach($userData as $index => $user) {

                $userEmail = $user['email'];

                PerchSystem::set_vars(array(
                    'class-name'   => $user['classuser'],
                    'class-banner' => $user['banner'],
                    'class-page'   => $user['_page']
                ));

                perch_content_custom('Faculty', array(
                    'page'       => '/faculty.php',
                    'sort'       => 'room',
                    'sort-order' => 'ASC',
                    'template'   => '_classdisplay_all.html',
                    'filter'     => array(
                        array(
                            'filter' => 'region',
                            'match'  => 'eq',
                            'value'  => 'primary'
                        ),
                        array(
                            'filter' => 'email',
                            'match'  => 'eq',
                            'value'  => $userEmail
                        )
                    )
                ));
            }

This was my final working template:

<perch:if exists="room">
    <div class="container">
        <perch:if exists="class-banner">
            <img src="<perch:content id="class-banner" />" alt="Class Banner" />
        </perch:if>

       <div class="overlay" style="display:none;"></div>
        <a href="<perch:content id="class-page" />">
        <div class="caption">
            <p class="category"> <perch:content id="title" /></p>
            <p class="text">
                <b>Room <perch:content id="room" /></b>:<br /> 
                       <perch:content id="class-name" /> 
            </p>
        </div>
        </a>
    </div>
</perch:if>

You're advice though will definitely come in handy for another project I'm working on...thank you!