Jump to content

Foreach page/field


mel47
 Share

Recommended Posts

Hi

I have a problem with a loop. I get repeated years. I understand, since I search for all member pages. But how I merged years (date_begin field, a number field)? Should be simple I guess, but I already spent hours on that...

	foreach ($categ_team as $categ) {
		$content .=  "<h2><a href='{$categ->url}'>{$categ->title}</a></h2>"; //MSc, stagiaire
	    $years = $page->find("template=member, featured=0, categ_team=$categ");
       foreach ($years as $y){  
		    $content .= "<h3>{$y->date_begin}</h3>";//les années 
	     	$member = $page->find("template=member, featured=0, date_begin={$y->date_begin}, categ_team=$categ"); 
		      foreach($member as $m) {
                   $content .= "<h4><a href='{$m->url}'>{$m->title}</a></h4>"; //nom de l'étudiant  
 	        	}
			}
	}

Thanks

Melanie

Link to comment
Share on other sites

Is this what you are looking for:

    foreach ($categ_team as $categ) {
        $content .=  "<h2><a href='{$categ->url}'>{$categ->title}</a></h2>"; //MSc, stagiaire
        $years = $page->find("template=member, featured=0, categ_team=$categ");
        foreach ($years as $y){  
            if($y !== $currentYear) $content .= "<h3>{$y->date_begin}</h3>";//les années 
            $member = $page->find("template=member, featured=0, date_begin={$y->date_begin}, categ_team=$categ"); 
            foreach($member as $m) {
                $content .= "<h4><a href='{$m->url}'>{$m->title}</a></h4>"; //nom de l'étudiant  
            }
            $currentYear = $y;
        }
    }

 

  • Like 1
Link to comment
Share on other sites

Unfortunately, it doesn't work.

Even if I define $currentYear='' at the beginning, I obtained the same result. I guess it's normal, since in $years, I'm searching id pages and not the value inside the field date_begin of this page? Maybe it's the thing I don't understand.

 

Link to comment
Share on other sites

Hello

My list look like that. The names are in the good categories, but they are repeated.
I tried foreach ($years->date_begin as $y) but it doesn't work also.

 

Stagiaire (categ_team)

2014 (date_year)

Fatma A (title)

2013

Juan B

Jorge A

2013

Juan B

Jorge A

Link to comment
Share on other sites

I'm guessing: The 2013 sub-list is showing up twice, because there are 2 members having this date_begin - and if you had 3 of them, it would show up three times...?

$years = $page->find("template=member, categ_team=$categ");

I think this PageArray will contain each year several times - one for each member having the same date_begin.

You could try

$years = $page->find("template=member, categ_team=$categ")->unique();

 

  • Like 1
Link to comment
Share on other sites

Yeah, it keep repeating, 2, 3, 4 times depending of how many members have this date.

Maybe it's not clear to me, but the result of $years is page id, not a year. So even if we add this ->unique(), I still get the same pageArray, already with unique values. Maybe it's my selector that is not good? I need to search for date_begin value of each member of a specific category and not just each page.

Link to comment
Share on other sites

BTW: 

Quote

$page->find(

Shouldn't that be

$pages->find(
Quote

... the result of $years is page id, not a year.

So that naming is rather misleading.

A proposal (written just here, not tested):

foreach ($categ_team as $categ) {
	$content .=  "<h2><a href='{$categ->url}'>{$categ->title}</a></h2>";
	
	$cat_members = $page->find("template=member, featured=0, categ_team=$categ");
	
	$years_start = array[];
	foreach ($cat_members as $cm){  
		$years_start[] = $cm->date_begin;
	};
	$years_start = $years_start->unique();

	foreach ($years_start as $y){  
		$content .= "<h3>{$y}</h3>";   //or apply some formatting to the $y value
		$members = $cat_members->find("date_begin={$y->date_begin}"); 
		foreach($members as $m) {
			$content .= "<h4><a href='{$m->url}'>{$m->title}</a></h4>";
		}
	}
}

Edit: Small correction.

  • Like 1
Link to comment
Share on other sites

Thanks. I get it! The way @ottogal wrote, it didn't work. However, with your idea of array and a search on forum, I mix it with the Ryan's answer.

So the final version is :

foreach ($categ_team as $categ) {
	$content .=  "<h2><a href='{$categ->url}'>{$categ->title}</a></h2>";
	
	$cat_members = $pages->find("template=member, featured=0, categ_team=$categ, sort=-date_begin");
	
	$years_start = [];
	foreach ($cat_members as $cm){  
		$years_start[$cm->date_begin] = $cm;
	}

	foreach ($years_start as $y){  
		$content .= "<h3>{$y->date_begin}</h3>";   //or apply some formatting to the $y value
		$members = $cat_members->find("date_begin=$y->date_begin"); 
		foreach($members as $m) {
			$content .= "<h4><a href='{$m->url}'>{$m->title}</a></h4>";
		}

	}
}

Thanks!

  • Like 2
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...