mel47 Posted November 24, 2016 Posted November 24, 2016 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
adrian Posted November 24, 2016 Posted November 24, 2016 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; } } 1
mel47 Posted November 26, 2016 Author Posted November 26, 2016 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.
adrian Posted November 26, 2016 Posted November 26, 2016 Maybe I don't fully understand what you are actually trying to achieve. Can you show us the output you are getting and what you instead want to get?
mel47 Posted November 28, 2016 Author Posted November 28, 2016 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
ottogal Posted November 28, 2016 Posted November 28, 2016 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(); 1
mel47 Posted November 29, 2016 Author Posted November 29, 2016 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.
ottogal Posted November 29, 2016 Posted November 29, 2016 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. 1
mel47 Posted November 30, 2016 Author Posted November 30, 2016 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! 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now