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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Control Break Processing?


Go to End


4 Posts   1687 Views

Avatar
_Vince

Community Member, 165 Posts

24 September 2010 at 12:33pm

Edited: 24/09/2010 12:34pm

Hi, I hope this is the right section

Say I have a pet shop, and I have the following table.

species  breed
=====  =====
Dog       Poodle
Dog       Labrador
Dog       Pekinese
Dog       Dalmatian
Cat        Persian
Cat        Tabby

and I want to display the table contents on a web page.

I'd like to have a big heading every time the species changes, something like

DOG
===
Poodle
Labrador
Pekinese
Dalmatian

CAT
===
Persian
Tabby

is there any way of doing this?

I know a way of doing it in MySQL but I also have an image for each animal, and I want to be able to do stuff like setWidth and so on. If I do a SQLQuery() I will lose that functionality.

I've also tried to merge two DataObjects but then I get the same records twice, once with the images, once without.

Any ideas?

Avatar
_Vince

Community Member, 165 Posts

25 September 2010 at 3:11pm

In the end, I adapted this from a reply I found on a MySQL forum. :)


		$sqlQuery = new SQLQuery();
		$sqlQuery->select = array(
		    "p.*",
			"@anum := if(@SubCategory = SubCategory, @anum + 1, 1) as row_num",
			"@SubCategory := SubCategory as dummy"
		);
		
		$sqlQuery->from = array("(SELECT @anum:=0) r,
			(SELECT @SubCategory:='') c, Products p");

		$sqlQuery->orderBy("CategoryID,SubCategory");
		
        $result = $sqlQuery->execute();

		$Products = new DataObjectSet();

		foreach($result as $row) {
			$Products->push(new ArrayData($row));
		}
		return $Products;

it returns a running sequence number for rows within the same subcategory.

And if I know the row number, I can go to the template and say

<% if row_num=1 %>
Do big heading
<% end_if %>

Avatar
ajshort

Community Member, 244 Posts

25 September 2010 at 5:51pm

There's a much nicer way of grouping records inbuilt into SilverStripe - see here for more information

Avatar
_Vince

Community Member, 165 Posts

28 September 2010 at 2:31pm

That IS nice! :)

Is there a way of having more than one child group?

At the moment it does

pets->dogs

whereas I need something like

products->pets->dogs