Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Pagination not working with a selected 'type' of children.

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

Page: 1
Go to End
Author Topic: 716 Views
  • BigMoose
    Avatar
    Community Member
    19 Posts

    Pagination not working with a selected 'type' of children. Link to this post

    I'm not desperately code savvy, but my website is coming along great non-the-less thanks to the forum posts from the past. However, I am struggling with one issue.

    Essentially, my website has a page which displays a video gallery grid - 3x3 grid of thumbnails appears, and the user can click on a video to watch. Additionally, the videos can be viewed based on what type they are. In my case, there are only 4 options: Film & Television, Corporate, Sports & Events, and Other.

    I've currently got things set up like this - there is a VideoHolder.php, and the child VideoPage.php.
    VideoPage.php has the following code:

    class VideoPage extends SiteTree {

       public static $db = array(
    // Variables to classify the videos
       'TypeFilmTV' => 'Boolean',
       'TypeCorporate' => 'Boolean',
       'TypeSportsEvents' => 'Boolean',
       'TypeOther' => 'Boolean',
       );

    // Down here I have all the new checkboxfields to appear in the cms.
    }

    The idea is that I can use <% if TypeFilmTV %> in my template to show only the ones of that type if it is selected.
    I used checkboxes instead of a dropdown because a video could be classified as more than one type, and there are not many types anyway.

    Now, in order that the user may select a video type to view on the page, I simply coded the link to add ?vidType=1 at the end of the link. These links are just a row of five buttons at the top of the page: "See All", "Film & Television", etc.

    I then added the following into VideoHolder.php

    class VideoHolder_Controller extends Page_Controller {
       
       public function Film()
    {return isset($_REQUEST['film']) && $_REQUEST['film'] == "1";}
       
       public function Corporate()
    {return isset($_REQUEST['corporate']) && $_REQUEST['corporate'] == "1";}
       
       public function Events()
    {return isset($_REQUEST['events']) && $_REQUEST['events'] == "1";}
       
       public function Other()
    {return isset($_REQUEST['other']) && $_REQUEST['other'] == "1";}
    }

    Then in my VideoHolder.ss template I could simply use a link as mentioned above with the added ?film=1 as an example and then write

    <% if Film %> // Or whatever type I am working with

    Anyway, all this works fine and dandy. The real problem I am having is when I use pagination now. In order to create my 3x3 grid, I have padding on the right side of each thumbnail except for the third one in each row. This is giving me the spacing I desire. I am accomplishing this by simply using:

    <% if MultipleOf(3) %>


    When pagination is used to view all the videos, it works perfectly. However, when a type is selected, pagination does not seem to 'see' the new position of each video. For example, suppose "video A" was third in the row, and "video B" appeared on page two when viewing all videos. Now when viewing just the videos labelled at "Film & Television" 'video A' appears in the second position. What will then happen is that 'video A' still does not have the padding on the right which had been controlled using <%MultipleOf(3)%>, even though it now appears second. Furthermore, "video B" is still showing on page 2 despite only 3 videos now appearing on page 1, and pagination coded to show 9 items per page. I hope this example is clear.

    My pagination function is in my VideoHolder_Controller and looks like this

    function Videos() {
        if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
        $SQL_start = (int)$_GET['start'];
        $doSet = DataObject::get(
          $callerClass = "VideoPage",
          $filter = "`ParentID` = '".$this->ID."'",
          $sort = "",
          $join = "",
          $limit = "{$SQL_start},9"
        );
       
        return $doSet ? $doSet : false;
       }

    I believe this code needs to be altered, but I have no idea how this code works. Again, I am not code savvy. I simply got this code from:
    http://doc.silverstripe.org/old/private:recipes:pagination and altered it very slightly for my needs. If anyone has an idea on how to alter this piece of code, and is willing to dumb it down for this shmuck I'd would be very appreciative. Thanking you in advance.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Pagination not working with a selected 'type' of children. Link to this post

    I'm not sure if I get this right, but what you seem to be doing in your template, is in fact loop the entire DataObjectSet - and based on <% if Film %> filter, decide whether to show an item or not. That would mean that item 5 from the set would still be item 5. even though items 1, 2, 3 and 4 are not displayed...

    Unfortunately paginating a filtered DataObjectSet isn't as straightforward. There's this tut on ssbits that might help you along:

    http://www.ssbits.com/tutorials/2010/paginating-a-filtered-dataobjectset/

  • BigMoose
    Avatar
    Community Member
    19 Posts

    Re: Pagination not working with a selected 'type' of children. Link to this post

    I believe that is correct. I changed the css, so that all the items in the 3x3 grid have exactly the same spacing. So I no longer use <% if MultipleOf(3) %> in my template. So that solved the issue of the spacing causing problems.

    I did look at that tutorial on ssbits, but did not have success with it. It's probably due to the way I have chosen to write my code for sorting the video types. I came close to having success with this code:

    function Videos() {
    if(!isset($_GET['film']) || !is_numeric($_GET['film']) || (int)$_GET['film'] < 1) $_GET['film'] = 0;
    $SQL_start = (int)$_GET['film'];
    $doSet = DataObject::get(
    $callerClass = "VideoPage",
    $filter = "`ParentID` = '".$this->ID."'",
    $sort = "",
    $join = "",
    $limit = "{$SQL_start},9"
    );

    return $doSet ? $doSet : false;
    }

    Again, I have zero idea what this code means. But changing it from $_GET['start'] to $_GET['film'] did partially work when I wanted to display pagination for just the film type videos. It would create the grid in the proper way even when using the old spacing and <%if Multiple Of(3) %>, but I would be left with links for page two, even though nothing existed on page two.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Pagination not working with a selected 'type' of children. Link to this post

    In your query ou're still retrieving every video object for the given page, and then you let your template decide what items to show. Default pagination is based on the entire DataObjectSet you retrieved, so that would account for the wrong number of pages.

    You might consider adding the videotype to the query itself. Something like this (I'm basing this on your code - there might be a better way to do this by reconstructing the page structure, but just for an idea...):

    function Videos($videotype='') {
       if(!isset($_GET['film']) || !is_numeric($_GET['film']) || (int)$_GET['film'] < 1) $_GET['film'] = 0;
          $SQL_start = (int)$_GET['film'];

          $myFilter = "`ParentID` = '".$this->ID."'";
          if ($videotype) $myFilter .= " AND `{$videotype}` = '1'";

          $doSet = DataObject::get(
          $callerClass = "VideoPage",
          $filter = $myFilter,
          $sort = "",
          $join = "",
          $limit = "{$SQL_start},9"
       );

       return $doSet ? $doSet : false;
    }

    ... and ...

    public function Corporate() {
       if (isset($_REQUEST['corporate']) && $_REQUEST['corporate'] == "1") return $this->Videos('TypeCorporate');
       else return false;
    }

    Then you could

    <% if Corporate %>
       <% control Corporate %>
          ...
       <% end_control %>
       ...
    <% end_if %>

    I've not tested this, it's just an idea

  • BigMoose
    Avatar
    Community Member
    19 Posts

    Re: Pagination not working with a selected 'type' of children. Link to this post

    Thank you martimiz for your time. I won't get a chance to try this for a couple days as I am out of town, but as soon as I can I will give it a go and report back. I really appreciate your taking the time to brainstorm this matter.

    716 Views
Page: 1
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.