Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to Display Data Results in 2 Well Formatted Rows

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

Page: 1 2
Go to End
Author Topic: 4126 Views
  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    How to Display Data Results in 2 Well Formatted Rows Link to this post

    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!      
          

  • UncleCheese
    Avatar
    4085 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

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

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

    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!

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

    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>

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

    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.

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

    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");
       }

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

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

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: How to Display Data Results in 2 Well Formatted Rows Link to this post

    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>

    4126 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.