Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » help about pagination

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2192 Views
  • bebabeba
    Avatar
    Community Member
    192 Posts

    help about pagination Link to this post

    Hi!
    I read everithig abou pagination in this forum and in documentation.
    I have a function that is caracterise by a query that show me 244 results.
    Function resturn query results.
    I need divide my result (10 for every page).
    I see http://doc.silverstripe.org/doku.php?id=tutorial:4-site-search but I can reate something correct to show my result in my template. Cany you help me?

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: help about pagination Link to this post

    Hello bebabeba!
    Are you still having trouble with pagination? If so, I could get it working today. I'm getting my Query results from an external database as well; I got some help from the IRC and I did it today.
    I created two functions, one that returns my whole data and other that returns just the range of data a want in every page. My first function is as follow:

    Public function GetSelectedAvailable(){
       global $product;
       $selectedprd;   
       global $SQL_start;
       global $Length;

       if (isset($product)) $selectedprd = $product;
       if (!isset($product)) $selectedprd = Session::get('product');
       if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
       $SQL_start = (int)$_GET['start'];

       $db = $this->connect();

       $sql="SELECT `AvailabilityMaster`.`Desc`, `Stylecodes`.`Style`, `AvailabilityRaw`.`Qty`, `AvailabilityRaw`.`Price`
       FROM (`AvailabilityRaw`
       LEFT JOIN AvailabilityMaster
       ON AvailabilityRaw.IDcode = AvailabilityMaster.IDcode)
       LEFT JOIN Stylecodes
       ON AvailabilityMaster.StyleCode = Stylecodes.code
       WHERE `AvailabilityRaw`.`product` ='".$selectedprd."'";

       $query = $db->query($sql);

       $result = new DataObjectSet();
       foreach($query as $row) {
       $result->push(new ArrayData($row));   
       }
       
       $Length = 10;
       //Set Page Limits
       $result->SetPageLimits($SQL_start,$Length,$result->Count());
       return $result;
    }

    I declared two global variables ($SQL_start and $Length) that I'm using for SetPageLimits, and I will use them also in my function that will return the range of items to display in each page. This function is the next one:

    Public function GetSelectedRange(){
       global $SQL_start;
       global $Length;
       $result = New DataObjectSet();
       $result = $this->GetSelectedAvailable();
       $result = $result->getRange($SQL_start,$Length);
       return $result;
    }

    In this function I just declared a new DataObjectSet, and then passed the result from the function that returns everything and then I filter it using $result->getRange(). And passing the values that are stored in the global variables.

    I hope this will help.
    Bye.

    2192 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.