Ostatnio napisałem taką prostą klasę Pager'a:
  1. <?php
  2.  
  3. class Pager
  4. {
  5.  public $Package=Array(); 
  6.  public $Redirect=False;
  7.  public $Allowed=Array('Previous' => False,
  8.  'Next' => False);
  9.  
  10.  private $Data=Array(); 
  11.  private $DisplayPerPage=15;
  12.  private $CurrentPage=0;
  13.  private $Marker='page';
  14.  private $PagesAmount=0;
  15.  
  16.  public function __construct($DisplayPerPage=Null, $Marker=Null)
  17.  {
  18. if(!is_null($DisplayPerPage))
  19.  if(is_int($DisplayPerPage))
  20. $this->DisplayPerPage=$DisplayPerPage;
  21.  else
  22. $this->Error('Class Pager needs integer value in first argument.', True);
  23.  
  24. if(!is_null($Marker))
  25.  if(is_string($Marker) || is_int($Marker))
  26. $this->Marker=$Marker;
  27.  else
  28. $this->Error('Class Pager needs string or integer value in third argument.', True);
  29.  
  30. $this->GetCurrentPage();
  31.  }
  32.  
  33.  public function ReturnPart($Data)
  34.  {
  35. $this->Assign($Data);
  36.  
  37. if($this->CurrentPage<=$this->PagesAmount && $this->CurrentPage>=0)
  38. {
  39.  $FirstElement=$this->CurrentPage*$this->DisplayPerPage;
  40.  $LastElement=$FirstElement+$this->DisplayPerPage-1;
  41.  $Counter=0;
  42.  
  43.  foreach($this->Data as $Key => $Value)
  44.  {
  45. if($Counter>=$FirstElement && $Counter<=$LastElement)
  46.  $this->Package[$Key]=$Value;
  47. $Counter++;
  48.  }
  49.  
  50.  return $this->Package;
  51. }
  52. else
  53.  if($this->Redirect==True)
  54. hreader('Location: '.$this->Redirect);
  55.  else
  56. $this->Error('Page '.$this->CurrentPage.' doesn't exists.');
  57.  }
  58.  
  59.  private function Assign($Data)
  60.  {
  61. if(is_array($Data))
  62.  $this->Data=$Data;
  63. else
  64.  $this->Error('Method Pager::Assign needs array value in first argument.', True);
  65.  
  66. $this->PagesAmount=ceil(count($Data)/$this->DisplayPerPage)-1;
  67. $this->Allowing();
  68.  }
  69.  
  70.  private function GetCurrentPage()
  71.  {
  72. if(is_numeric($_GET[$this->Marker]))
  73.  $this->CurrentPage=$_GET[$this->Marker];
  74. else
  75.  $this->CurrentPage=0;
  76.  }
  77.  
  78.  private function Allowing()
  79.  {
  80. if($this->CurrentPage>0 && $this->CurrentPage<=$this->PagesAmount)
  81.  $this->Allowed['Previous']=True;
  82. if($this->CurrentPage>=0 && $this->CurrentPage<$this->PagesAmount)
  83.  $this->Allowed['Next']=True;
  84.  }
  85.  
  86.  private function Error($Communicate, $Die=False)
  87.  {
  88. if($Die==True)
  89.  die('<code>'.$Communicate.'</code>');
  90. else
  91.  echo('<code>'.$Communicate.'</code>');
  92.  }
  93. }
  94.  
  95. ?>


Jak widać dokumentacja jest obszerna tongue.gif
Sposób użycia:
  1. <?php
  2.  
  3. include('pager.class.php');
  4. $Pager=new Pager();
  5.  
  6. $Array=$_SERVER;
  7. $Part=$Pager->ReturnPart($Array);
  8.  
  9. echo '<pre>';
  10. print_r($Part);
  11. print_r($Pager->Allowed);
  12. echo '</pre>';
  13.  
  14. ?>