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.

Data Model Questions /

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

How to Display Data Results in 2 Well Formatted Rows


Go to End


12 Posts   6126 Views

Avatar
Andrew Houle

Community Member, 140 Posts

14 January 2009 at 10:43am

Edited: 14/01/2009 10:44am

I'm not sure the best way to word this, but here goes:

I want to display my data results from a query in 2 rows preferably using css divs, but I'd take it in two table rows at this point.

Here's where I'm at right now. I have a simple function in the Page.php controller class that gets all the SponsorPage results:

function getSponsors() {
		$sp = DataObject::get("SponsorPage", "", "", "", "");
  		return  $sp;
	}

Then I have the following in my template that displays the info:

<h1>Sponsors</h1>
            <ul>
                <% control getSponsors %>
                	<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a></li>
                <% end_control %>
            </ul>   

Being that you can't use the modulus operator from w/in templates in SS, how can I manipulate the data into two columns. I figure I have to do something in the Page controller class, but I don't even know where to start. I would like the code to produce either two divs like:

<div class="col float-left">
			<h1>Sponsors</h1>
			<ul>				
				<li><a href="data-model-questions/editpost/252268#">Campus Door</a></li>
				<li><a href="data-model-questions/editpost/252268#">Engineer Your Life</a></li>	
				<li><a href="data-model-questions/editpost/252268#">Key Bank</a></li>						
				<li><a href="data-model-questions/editpost/252268#">Daemen College</a></li>
			</ul>			
		</div>

<div class="col float-left">
			<h1>Sponsors</h1>
			<ul>				
				<li><a href="data-model-questions/editpost/252268#">Campus Door</a></li>
				<li><a href="data-model-questions/editpost/252268#">Engineer Your Life</a></li>	
				<li><a href="data-model-questions/editpost/252268#">Key Bank</a></li>						
				<li><a href="data-model-questions/editpost/252268#">Daemen College</a></li>
			</ul>			
		</div>

or a table like:
<h1>Sponsors</h1>
                       <table>			
				<tr><td><a href="data-model-questions/editpost/252268#">Campus Door</a></td>
				<td><a href="data-model-questions/editpost/252268#">Engineer Your Life</a></td></tr>	
				<tr><td><a href="data-model-questions/editpost/252268#">Key Bank</a></td>						
				<td><a href="data-model-questions/editpost/252268#">Daemen College</a></td></tr>
			</table>	

Any help you could give would be very much appreciated. TY!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 January 2009 at 11:05am

First things first, you can clean up your function a bit.

$sp = DataObject::get("SponsorPage", "", "", "", "");
return $sp;

becomes

return DataObject::get("SponsorPage");

As for formatting your rows, you can't use a modulus, but you can use the $EvenOdd control variable.

<li class="$EvenOdd">

Avatar
Andrew Houle

Community Member, 140 Posts

15 January 2009 at 3:04am

Hey Aaron,

Good idea, that would probably be the easiest way to do it, and I actually now how to get that done :)

Do you know a way to count the total rows, divide that in half, then display the first half in the first div and the second half in a second div? Or is it just not worth it?

Thanks for the help!

Avatar
Carbon Crayon

Community Member, 598 Posts

15 January 2009 at 3:40am

Edited: 15/01/2009 4:20am

you could probably do something like this, it's a little crude but should work:

function Sponsers(){
      return DataObject::get("SponserPage");
}

function Row1(){

     $Half = ($this->Sponsers()->Count()) /2;

     return DataObject::get("SponserPage", "", "",  "", "0, $Half");
}

function Row2(){

     $Total = ($this->Sponsers()->Count()) ;
     $Half = ($Total / 2)+1;

     return DataObject::get("SponserPage", "", "",  "", "$Half, $Total");

}

Then just do this in your template:

<div id="row1">
<% control Row1 %>

<% end_control %>
</div>

<div id="row2">
<% control Row2 %>

<% end_control %>
</div>

Avatar
Andrew Houle

Community Member, 140 Posts

15 January 2009 at 4:11am

Hi aram,

Thanks for your help, that's exactly what I'm after. I had to make a couple of slight changes to your code to get to where I could test it. Mainly add a semicolon to the end of the DataObject lines and spell Sponsors w/ an 'o'. However, I still can't get it to work the way I want. I'm just working with the Row1 function for now. I can get it to print the sponsors if I take out the divide by 2 part. But if I leave that in it causes an error page. I can manipulate the limit w/ other math functions, ie $Half = ($this->Sponsors()->Count()) - 1;

Any ideas why $Half = ($this->Sponsors()->Count()) / 2; causes it to break down?

I'll keep looking into it.

Avatar
Andrew Houle

Community Member, 140 Posts

15 January 2009 at 4:19am

Nevermind, I got it :) For the sake of completion here is what I found out. You have to round the first column up using the ceil() function and the second column down using the floor() function just in case you get a number with a remainder. So the Row1() function would look something like:

function Row1() {
		$Split = ($this->Sponsors()->Count()) / 2;
		$Half = ceil($Split);
		return DataObject::get("SponsorPage", "", "", "", "0, $Half");
	}

Avatar
Carbon Crayon

Community Member, 598 Posts

15 January 2009 at 4:25am

ah great, that will come in handy, thanks for posting :) Glad I could help, just gotta get control of my typos now! hehe

Avatar
Andrew Houle

Community Member, 140 Posts

15 January 2009 at 4:33am

Thanks so much for pointing me in the right direction. Here is my final code for anyone who may try this later:

Class Excerpt...

function Sponsors() {
		return DataObject::get("SponsorPage");
	}
	
	function Row1() {
		$Half = ceil(($this->Sponsors()->Count()) / 2);
		return DataObject::get("SponsorPage", "", "", "", "0, $Half");
	}
	
	function Row2() {
		$Total = ($this->Sponsors()->Count()) ;
		$Half = floor(($Total / 2))+1;
		return DataObject::get("SponsorPage", "", "", "", "$Half, $Total");
	}

Template Excerpt...

<div class="col float-left">
			<h1>Sponsors</h1>
			<ul>				
				<% control Row1 %>
                	<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a></li>
                <% end_control %>
			</ul>			
		</div>
		
		<div class="col float-left">
			<h1>Sponsors</h1>
            <ul>
                <% control Row2 %>
                	<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a></li>
                <% end_control %>
            </ul>            
		</div>

Go to Top