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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

[SOLVED] Grouping Output


Reply

3 Posts   1527 Views

Avatar
zenmonkey

25 August 2009 at 8:34am (Last edited: 27 August 2009 8:42am), Community Member, 528 Posts

I have a list of Retailers and Distributors I want to output in the template sorted by continent, country and category

Below is the Data Object

class ProductSeller extends DataObject
{
   static $db = array (
      'SellerName' => 'Text',
      'SellerDescription' => 'Text',
      'SellerCategory' => "Enum('Retail, Distributor, Web')",
      'SellerLocation' => 'Text',
      'SellerContinent' => "Enum('NorthAmerica, SouthAmerica, Europe, Asia, Australia')",
      'SellerURL' => 'Text',
      'SellerPhone' => 'Text'
   );
   
   static $has_one = array (
      'WhereToBuyPage' => 'WhereToBuyPage'
   );
   
   public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('SellerName'),
         new TextareaField('SellerDescription'),
         new DropdownField('SellerCategory','SellerCategory', singleton('ProductSeller')->dbObject('SellerCategory')->enumValues()),
         new CountryDropdownField('SellerLocation'),
         new DropdownField('SellerContinent','Continent', singleton('ProductSeller')->dbObject('SellerContinent')->enumValues()),
         new TextField('SellerURL'),
         new TextField('SellerPhone')
      );
   }
   
}

In the template I can group the SellerContinent and SellerCategory by if statements, however is there an easy way to group the countries without manually making an if statements for every country.

I wrote a function thats sorts the Data by country so worse comes to worse I can use the country as a class and jQuery the whole thing and just give the JavaScript-less people a sorted list.

function sortByCountry(){
      $countrys = DataObject::get("ProductSeller","", "SellerLocation");
      return $countrys;
   }

Thanks

Avatar
UncleCheese

25 August 2009 at 9:09am 4085 Posts

Try this:

WhereToByPage.php

function Countries()
{
$countries = new DataObjectSet();
$results = DB::query("SELECT DISTINCT SellerLocation FROM ProductSeller ORDER BY SellerLocation ASC");
if($results) {
foreach($results as $result) {
$country = $result['SellerLocation'];
$countries->push(new ArrayData(array(
'Country' => $country,
'Sellers' => DataObject::get("ProductSeller","SellerLocation = '$country'")
)));
}
return $countries;
}

return false;
}

WhereToBuyPage.ss

<% control Countries %>
<h3>$Country</h3>
<ul>
<% control Sellers %>
<li>$SellerName</li>
<% end_control %>
</ul>
<% end_control %>

Can't promise it's free of syntax errors, but that's the idea.

Avatar
zenmonkey

27 August 2009 at 8:43am Community Member, 528 Posts

Awesome. UncleCheese, You're the Best!