Jump to content

Problem with multiple foreach statements...


PhotoWebMax
 Share

Recommended Posts

Ok, I am busy working on my second PW site. Things are going pretty well. Sort of fun actually  :)

I am having problems with a page that uses two instances of an foreach loop. I have a grid system of presentation CSS classes for listing "identity cards" for Staff Members and a similar system for listing Board of Directors

My code looks like this:

<!--begin presenting dynamic list of staffCards -->
<hr/>		
<h2>Staff</h2>
<?foreach ($page->children as $c):?>
<div class="staffCards">
	<h4><?=$c->staffName;?></h4>
	<h5><?=$c->staffAddress;?></h5>
	<h5><?=$c->staffPhone;?></h5>
	<img src="<?=$c->image->url;?>" alt="<?=$c->image->description;?>">
	<p><?=$c->staffDescription;?></p>
	</div><!--staffCards ends -->
	<? endforeach; ?>
<hr/>
<h2>Board of Directors</h2>
<?foreach ($page->children as $directors):?>
<div class="directorsCards">
	<h4><?=$directors->directorsName;?></h4>
	<img src="<?=$directors->image->url;?>" alt="<?=$directors->image->description;?>">
	<p><?=$directors->directorsDescription;?></p>
	</div><!--directorsCards ends -->
	<? endforeach; ?>
			
</div> <!--mainColumn ends -->

For the most part this is working. When I add child pages to the About page using a template I call staffCardTemplate for the  Staff members I get a nice list of CSS styled Staff id cards. When I then add a child page using a template I call directorsCardTemplate for the Board of Directors I get a different CSS styled list of Board of Directors id cards. I am using fields that are similar for both templates, but with different names so I can style the presentation differently. 

The Staff cards are presented dynamically in a vertical row and then the Board of Directors cards are below the Staff cards, all on the same page.

The issue is that when I create a new Board of Directors card the page is also generating an extra blank box in the Staff cards section of the page. There is no content in this box but the border of the class div is showing? Not wanted...

I suspect this line of code is wrong:

<?foreach ($page->children as $directors):?>

How close (or far) am I to getting this right?

Thanks!

Link to comment
Share on other sites

I suspect this line of code is wrong:

<?foreach ($page->children as $directors):?>

I don't think that's the problem. I think you're having issues here:

<?foreach ($page->children as $c):?>
 and
<?foreach ($page->children as $directors):?>

They are the same loop, with the same set of children being iterated so it makes sense that every time you add a directors page you get another box being drawn, but it's empty because your fields (with the exception of image) are named differently.  Try using a selector to specify the template. Maybe:

<?foreach ($page->children("template=staffCardTemplate") as $c):?>
... Rest of Code 
<?foreach ($page->children("template=directorsCardTemplate") as $directors):?>

That way your code only grabs children of the appropriate template when iterating. 

  • Like 2
Link to comment
Share on other sites

One other thing though...

I have my site about 50% completed. The design is there. The topNav menu works. The main templates work, and I am dynamically making content blocks (pages) for items like Staff, Directors and Programs appear (listed) on their relevant parent pages. 

Right now I am working on the sidebar and am a little confused. I am using most of the code on the PW default installation as a starting point for my sidebar template. I can make Fields for a headline and some intro text.

But what is happening with my sidebar currently is that links to the Staff Members and Board of Directors cards are appearing in the sidebar menu. I do not want these links to appear in the sidebar menu. Editing these card page links to "Hidden: Excluded from lists and searches" removes their links in the sidebar but also prevents their content from showing in the main pages I want them to appear in.

I am guessing there is something extra I can add to the foreach statements that would allow the content to show but also prevent the unwanted links from showing up in the sidebar?

Link to comment
Share on other sites

@adrian,

Thanks, that worked. I had to double check the Selectors section of the API but I got it working the way I want.

@diogo,

I know I need to look into the whole template!=name component of managing templates but I have not gotten there yet...

Question: adding the include=hidden to the selector of the foreach statements works well for me. The one issue I see is that any client that uses my system will have to know and remember that he/she will need to check the Hidden box in the Settings pane when editing or creating a page. Not the end of the world but it would be nice to have this check box checked (Hidden from lists and searches) as a default for this section of pages. Is there a way of doing this?

Thanks!

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...