Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Nested control blocks


Reply


2 Posts   1444 Views

Avatar
Laurie

Community Member, 21 Posts

9 June 2010 at 10:22pm

I'm trying to create a series of regional pages that then list projects grouped by country in the particular region.

- I have a country table in the db with the following three fields: ID, Name, RegionPageID.
- I also have a projectpage table in the db with info about the projects.
- And finally I have a projectpage_country table that relates the two: ID, ProjectPageID, CountryID

I'm getting stuck on writing the foreach loop that gets the project info and combines it with the country info. None of the examples I'm finding show how to do this with two DataObjects.

   function Countries() {
      // get countries for the given region
      $countryList = DataObject::get (
         $callerClass = 'Country',
         $filter = "RegionPageID = '{$this->ID}'",
         $sort = 'name ASC'
      );
      
      $countryListIterator = $countryList->getIterator();
      
      // loop through countries
      foreach($countryListIterator as $country) {
         // if the country has projects associated with it
         // return the country name to the template along with the related projects (as a dataobject?)
      }
      
      // return something here
   }
   
   function Projects($countryID) {
      $where = "projectpage_country.countryID = '$countryID'";
      $join = "RIGHT JOIN projectpage_country ON ProjectPage_Live.ID = projectpage_country.projectPageID";
      return DataObject::get('ProjectPage', $where, 'endDate DESC, title ASC', $join);
   }

Then in the template...

      <% control Countries %>
      <h3>$CountryName</h3>
         <% control Projects %>
            <% if first %>
               <ul>
            <% end if %>
                  <li><a href="$Link">$Title</a> ($startDate.Year&ndash;$endDate.Year)</li>
            <% if last %>
               </ul>
            <% end if %>
      <% end_control %>

Any help would be most appreciated. Thanks.

Cheers,
Laurie

Avatar
Tonyair

Community Member, 81 Posts

10 June 2010 at 9:37am

function Countries() {
// get countries for the given region
$countryList = DataObject::get (
$callerClass = 'Country',
$filter = "RegionPageID = '{$this->ID}'",
$sort = 'name ASC'
);
...
$countryList->Projects = Projects($countryID);
$countryList->Projects-> type = 'project';
}

Then u can use recursive function in page controller that will remake that object to one array of objects like that:

$a = array ( $class1, $class1-1, $class2, $class2-2);

Where $class1-1 and $class2-2 is $countryList->Projects;

<% control RecursiveFunctionName() %>
<% if type == project %>
$projectPageID <!-- it's a Project --!>
<% else %>
         $name <!-- it's a Country --!>
<% end_if %>
<% end_control %>

Something like that =)