Would you like to be notified for every new articles? Please click HERE to subscribe for newsletter.

PHP Class For Paging

  • Posted on: 12 February 2011
  • By: admin

Creating paging in traditional way can be troublesome, especially if you have to create it for many pages. This class will help you to solve that problem. By using this class you can create pagination with PHP and MySQL easily with just a few lines of code.

Basic Usage

These are the steps to create paging using this class.

  • Include the class.
    <?php
    include("nicePaging.php");
    ?>
  • Create the instance.
    <?php
    $paging
    =new nicePaging($con);
    ?>

    * Note : $con is MySQL connection link variable. If you omitt it, the last connection to MySQL will be used.

  • Define the rows per page.
    <?php
    $rowsPerPage
    =10;
    ?>
  • Execute query.
    <?php
    $result
    =$paging->pagerQuery("SELECT id, title FROM sample"$rowsPerPage);
    ?>

    * Note : The $result variable is the same like the result from mysql_query function, so you can fetch it using mysql_fetch_assoc or mysql_fetch_row.

  • Display the paging.
    <?php
    $link
    ="sample1.php"// Page name

    // Create links for paging

    echo $paging->createPaging($link);
    ?>

Also don't forget to use this CSS for styling the paging.

ul.nice_paging{
  padding:10px 0 10px 0;
  text-align:center;
  font-weight:bold;
  color:#777;
}
 
ul.nice_paging li{
  padding:3px 7px 3px 7px;
  margin:3px;
  border:1px solid #aaa;
  background-color:#eee;
  display:inline;
  list-style:none;
}
 
ul.nice_paging li.current{
  background-color:#369;
  color:#fff
}
 
ul.nice_paging li a{
  text-decoration:none;
  font-weight:bold;
  color:#777;
}

See the example here.

Using Separator

The default separator between the URL and the query string for paging is "?". If you have a URL like "sample2.php?option=test", you have to specify the separator with "&" character like the sample code below.

<?php
$link
="sample2.php?option=test"// Page name

$paging->setSeparator("&"); //Separator between page name and query string for page number

// Create links for paging

echo $paging->createPaging($link);
?>

See the example here.

Number of links per page

The default maximum number of links displayed per page is 10. You can also change it by using the code below

<?php
$link
="sample3.php"// Page name

$paging->setMaxPages(3); //Number of paging links that will be displayed per page

// Create links for paging

echo $paging->createPaging($link);
?>

See the example here.

Full Example

See the full example here.

AttachmentSize
Binary Data Source code and example4.05 KB

Comments

Thank u, this realy save my time :D

Hey Well paging was very popular in the olden days but later the people had resorted to texting using the network sms. And now the people have resorted to blackberry messengers, android applications like whatsapp, kik , we chat etc phone systems for small business !!

Add new comment

Limited HTML

  • Allowed HTML tags: <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.