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.

General Questions /

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

help about pagination


Reply


2 Posts   2312 Views

Avatar
bebabeba

Community Member, 193 Posts

7 March 2009 at 12:32am

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?

Avatar
Jardiamj

Community Member, 17 Posts

10 March 2009 at 6:37am

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.