webhoes Posted January 9, 2018 Share Posted January 9, 2018 Hello, I am struggeling with the following. It concerns adding new content in the backend of the site. I have a template region with two fields. Country and provence wich is a child of country. Country is an amselect on the region template. I can select one or more countries. Now I would like to see only the children of selected countries (is present) in the selectbox provence. I looked at the possibilities of the query, but could not quite figure it out. Any tips? 1 Link to comment Share on other sites More sharing options...
kongondo Posted January 9, 2018 Share Posted January 9, 2018 2 Link to comment Share on other sites More sharing options...
webhoes Posted January 9, 2018 Author Share Posted January 9, 2018 Thanks for the link @kongondo With parent=page.verspreiding When I output is in the template I do get something wierd. I expect the output to be like: Afghanistan, China, Mexico(Campeche, Chiapas) but I get Afghanistan, China, Mexico(Campeche, Chiapas, Oaxaca). I get all children, not just those I selected on the page. <?php foreach($page->verspreidingen as $item){ // all countries echo "<a href='". $item->url . "'>" . $item->title . "</a>"; if(count($item->children) > 0 ){ // states of a country echo " ("; foreach ($page->sub_verspreiding("has_parent=$item") as $sub){ // foreach state on the page that is child of a country if($sub == $item->children->last()) { echo "<a href='" . $sub->url . "'>" . $sub->title . "</a>";} else { echo "<a href='" . $sub->url . "'>" . $sub->title . "</a>, ";} } echo ")"; } if($item != $items->last()) echo ", "; } ?> Link to comment Share on other sites More sharing options...
Macrura Posted January 9, 2018 Share Posted January 9, 2018 $item is the page object for the country, so will return true on your if statement, even if no states are selected. Link to comment Share on other sites More sharing options...
webhoes Posted January 9, 2018 Author Share Posted January 9, 2018 Yes, that is the way it should be. But I also get all states of that country (child/parent relation) even is they are not selected on the page. Link to comment Share on other sites More sharing options...
Macrura Posted January 9, 2018 Share Posted January 9, 2018 no, it shouldn't be that way, because item will always return true if the country has states. you are even echoing an opening parenthesis before checking to see what if anything is selected on the page itself. the logic needs to be to check both if the country has states AND if there are states selected on the page; you don't even need to use has_parent, because if you are using dependent selects that has already taken care of filtering the selectable options by the country select field. Link to comment Share on other sites More sharing options...
webhoes Posted January 9, 2018 Author Share Posted January 9, 2018 3 hours ago, Macrura said: you are even echoing an opening parenthesis before checking to see what if anything is selected on the page itself. Totally overlooked this I will give it another thought tomorrow and apply better logic Link to comment Share on other sites More sharing options...
webhoes Posted January 11, 2018 Author Share Posted January 11, 2018 This seems to work Perhaps it could be more efficient. <?php foreach($page->verspreidingen as $country){ // all countries echo "<a href='". $country->url . "'>" . $country->title . "</a>"; $s = null; $s_out = null; $states = null; $sarray = new PageArray(); foreach ($page->sub_verspreiding as $state) { // foreach state on the page that is child of a country if ($state->parent == $country) $sarray->add($state); } foreach($sarray as $s){ $s_out .= "<a href='". $s->url . "'>" . $s->title . "</a>"; if($s != $sarray->last()) $s_out .= ", "; } if(isset($s_out)) echo " (" . $s_out . ")"; if($country != $page->verspreidingen->last()) echo ", "; } ?> 2 Link to comment Share on other sites More sharing options...
webhoes Posted January 22, 2018 Author Share Posted January 22, 2018 This is the efficient version. By using substring I avoid creating new page arry in the foreach loop. <?php $text = ""; foreach($page->verspreidingen as $country){ // all countries $text .= "<a href='". $country->url . "'>" . $country->title . "</a>"; $s_out = null; foreach ($page->sub_verspreiding as $state) { // foreach state on the page that is child of a country if ($state->parent == $country) { $s_out .= "<a href='". $state->url . "'>" . $state->title . "</a>, "; } } substr($s_out, 0,-2); if(isset($s_out)) echo " (" . $s_out . ")"; $text .= ", "; } substr($text, 0,-2); echo $text; ?> 1 Link to comment Share on other sites More sharing options...
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