PHP Classes
elePHPant
Icontem

File: index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cristian Radulescu  >  Page Numbers  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Usage example
Class: Page Numbers
Get pages to split listings in multiple pages
Author: By
Last change:
Date: 7 years ago
Size: 1,867 bytes
 

Contents

Class file image Download
<?php
   
//generate array
   
$myResults = range(0, 710);

   
$resultsPerPage = 30; //30 results on a page
   
$totalPages = ceil(count($myResults)/$resultsPerPage); //calculate total number of pages
   
echo "total pages = " . $totalPages . "<br />";

   
$page = $_GET['p']; //get page number
   
echo "page number = " . $page . "<br /><br />";

   
//display results
   
$sliceStart = ($page-1)*$resultsPerPage;
   
$slice = $resultsPerPage;
    echo
"<pre>";
   
print_r(array_slice($myResults, $sliceStart, $slice));
    echo
"</pre>";
   
   
    include(
"pageNumbers.class.php");
   
   
//parameters: $page = current page number
    //$totalPages = total number of pages
    //3 = numbers to display on the left and right of the current page number
   
$pag = new pageNumbers($page, $totalPages, 5);

   
//the first and the last page number will always be displayed
    //we need a separator between the first/last page number and the middle page numbers
   
$separator = "...";
    foreach(
$pag->numbers as $pageNumber=>$type)
    {
       
//each number has a type
        //there are 4 types:
        // "current" - the curent page number;
        // "link" - link to other page numbers
        // "separatorAfter" - first line number when it needs the separator (separator after number)
        // "separatorBefore" - last line number when it needs the separator (separator before number)
       
switch($type)
        {
            case
"current": echo '&nbsp;' . $pageNumber . '&nbsp';
                break;
               
            case
"link": echo '&nbsp<a href="index.php?p=' . $pageNumber . '">' . $pageNumber . '</a>&nbsp;';
                break;
               
            case
"separatorAfter": echo '&nbsp<a href="index.php?p=' . $pageNumber . '">' . $pageNumber . '</a>&nbsp;' . $separator . '&nbsp;';
                break;
               
            case
"separatorBefore": echo '&nbsp;' . $separator . '&nbsp;<a href="index.php?p=' . $pageNumber . '">' . $pageNumber . '</a>';
                break;
        }
    }
?>