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 make Pagination on my PageHolder


Go to End


7 Posts   8239 Views

Avatar
WebSpilka

Community Member, 89 Posts

7 February 2009 at 11:35pm

I want to do something similar to http://silverstripe.org/modules?start=10
can share the code how to do Pagination with "Previous page" and "Next page" links

Avatar
Carbon Crayon

Community Member, 598 Posts

8 February 2009 at 2:37am

Edited: 08/02/2009 2:44am

Hi Fiord, you need to do something like this:

mysite/code/Page.php


//functions in Page_controller, gets a list of objects and paginates them
function getObjects() {
  if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
  $SQL_start = (int)$_GET['start'];
  $doSet = DataObject::get(
    $callerClass = "$PageClass", // enter the Object type (e.g. 'MyPageType')
    $filter = "`ParentID` = '".$this->ID."'",
    $sort = "Date DESC", 
    $join = "",
    $limit = "{$SQL_start}, $itemsPerPage" // enter the number of items per page (int)
  );
 
  return $doSet ? $doSet : false;
}

mysite/templates/Layout/Page.ss


<% if getObjects.MoreThanOnePage %>
  <p class="pageNumbers">
  <% if getObjects.PrevLink %>
    <a href="$getObjects.PrevLink"><< Prev</a> | 
  <% end_if %>
 
  <% control getObjects.Pages %>
    <% if CurrentBool %>
      <strong>$PageNum</strong> 
    <% else %>
      <a href="$Link" title="Go to page $PageNum">$PageNum</a> 
    <% end_if %>
  <% end_control %>
 
  <% if getObjects.NextLink %>
    | <a href="$getObjects.NextLink">Next >></a>
  <% end_if %>
  </p>
<% end_if %>

Avatar
bebabeba

Community Member, 193 Posts

17 February 2009 at 11:38pm

H!
I need to do something like this but data that I need to paginate, are the result of my query.
Can you help me to print 10 result for page please?I'm unable to realize correctly..
This is the function with my query.

function Conn()
{ global $db_pandora;
$record=$db_pandora->query('SELECT * FROM mm_ville LIMIT 0, 30');

$rec=array();

while($rec[]=$record->nextRecord())
{ }

$doSet = new DataObjectSet();

foreach($rec as $key)
{ $record = array(
'Id' => $key['id'],
'Nome' => $key['name'],
);
doSet->push(new ArrayData($record));}

return $doSet;
}

Avatar
marc79

Community Member, 65 Posts

25 June 2011 at 1:02am

Hi Aram.

Thanks for your post. I have implemented your code on my site and have run into an error when I click through the second page:

"StartTag: invalid element name"

The code I am using is:

PhotoGalleryPage.php

function getObjects() { 
	if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0; 
	$SQL_start = (int)$_GET['start']; 
	$doSet = DataObject::get("PhotoGallery", "", "PhotoDate DESC", "", "{$SQL_start}, 4" );

	return $doSet ? $doSet : false; 
}	

PhotoGalleryPage.ss

<div id="photoList">	
    	<% control getObjects %>
            	<div class="photoFrame">
        	    	<div class="photoImageField">$Photo.SetRatioSize(270,203)</div>
			<div class="photoCaptionField">$PhotoCaption</div>
			<div class="photDateField">Taken: $PhotoDate.format(d-M-y)</div>
			<div class="clear"></div>
		</div>	
   	<% end_control %>		
    	
    	<% if getObjects.MoreThanOnePage %> 
		<p class="pageNumbers"> 
		<% if getObjects.PrevLink %> 
		<a href="$getObjects.PrevLink"><< Prev</a> | 
		<% end_if %>
		<% control getObjects.Pages %> 
		<% if CurrentBool %> 
		<strong>$PageNum</strong> 
		<% else %> 
		<a href="$Link" title="Go to page $PageNum">$PageNum</a> 
		<% end_if %> 
		<% end_control %>
		<% if getObjects.NextLink %> 
		| <a href="$getObjects.NextLink">Next >></a> 
		<% end_if %> 
		</p> 
		<% end_if %>
	</div>   
    
</div>

Everything seems to be ok. Any suggestions on what might be causing the issue would greatly received.

Many thanks

Marc

Avatar
neilcreagh

Community Member, 136 Posts

23 September 2011 at 8:43pm

Thank you Aram! This saved me hours.

Avatar
Dimenicius

Community Member, 8 Posts

6 May 2013 at 11:50am

Does it work on SS3? the getObjects is working fine, but the Next/Prev buttons aren't showing up

Avatar
Nivery

Community Member, 6 Posts

17 July 2013 at 3:01am

Dimenicius, check out the SS3 documentation for PaginatedList.