Jump to content

Recommended Posts

Posted

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?

  • Thanks 1
Posted

Thanks for the link @kongondo

With parent=page.verspreiding

image.thumb.png.48b5b90d9bec5e82ec5bf52ea68d0383.png

 

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 ", ";

}
?>
Posted

$item is the page object for the country, so will return true on your if statement, even if no states are selected.

 

Posted

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.

Posted

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.

Posted
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

Posted

This seems to work :D 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 ", ";
}
?>
  • Like 2
  • 2 weeks later...
Posted

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;
?>
  • Like 1

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
  • Recently Browsing   0 members

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