PHP
  Home arrow PHP arrow Page 4 - Previous or Next? Paginating Records with PHP, part 4
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Previous or Next? Paginating Records with PHP, part 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2005-05-17


    Table of Contents:
  • Previous or Next? Paginating Records with PHP, part 4
  • A quick look at the class source code
  • Creating the database table
  • Paging MySQL records: putting the "Pager" class into action
  • Extending the use of the "Pager class": more examples to come

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Previous or Next? Paginating Records with PHP, part 4 - Paging MySQL records: putting the "Pager" class into action
    ( Page 4 of 5 )

    Before showing any examples, a disclaimer is in order. Since we need to connect to the MySQL server, I will use a hypothetical MySQL class to perform the server connection. So, if you're seated on the procedural chair, don't worry. I'll take a look at that approximation too. Having said that, let's show the first example, first defining a simple default template file, as listed below:

    <table>
      <tr class="header">
        <td>Song Id</td><td>Song Name</td><td>Song Author</td>
      </tr>
      <tr>
        <td>{data0}</td><td>{data1}</td><td>{data2}</td>
      </tr>
    </table>

    That's all that we need to format the result set. Definitely, you'll agree that this template file is very simple. Now, let's continue defining some CSS rules, in order to style the records and the paging links:

    table {
      width: 50%;
      background: #ffc;
      font: normal 11px "Verdana", Arial, Helvetica, sans-serif;
      color: #000;
      text-align: center;
      border: 1px solid #000;
    }

    tr.header {
      background: #fc0;
      font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
      color: #000;
    }

    td {
      width: 33%;
      padding: 2px;
    }

    #paginglinks {
      position: absolute;
      top: 180px;
      left: 320px;
      font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
      color: #00f;
    }

    Finally, let's implement our "Pager" class, including the paging class file, a MySQL abstraction class, and the instantiation of the corresponding objects:

    <?php
    // include class files
    require_once('pagerclass.php');
    require_once('mysqlclass.php');

    // instantiate a new MySQL object
    $db=&new MySQL('dbhost','username','password','songs');

    // instantiate a new Pager object
    $pager=&new Pager($db->getConnectionId(),'SELECT * FROM songs','paginglinks',5);

    // display paginated result set
    echo $pager->displayRecords($_GET['page']);

    ?>

    Notice from the above example, that I've specified the display of five records per page, feeding the class with that incoming value.

    For those Web developers connecting to MySQL using a procedural method, the above code can be rewritten as follows:

    <?php
    // include the pager class
    require_once('pagerclass.php');

    // connect to MySQL
    $conId=@mysql_connect('dbhost','username','password') or die ('Error connecting to the server: '.mysql_error());

    // select database
    mysql_select_db('songs') or die ('Error selecting database');

    // instantiate a new pager object
    $pager=&new Pager(&$conId,'SELECT * FROM songs','paginglinks',5);

    // display records and paging links
    echo $pager->displayRecords($_GET['page']);

    ?>

    Whatever method you use to connect to MySQL, the rendered output would look similar to this:

    Not too bad, eh? Please notice how we've formatted database records and paging links, simply by using a three line template file. However, this is not the end. What if we change our mind and want to use another template file to build a different look and feel? Just go to the next page to find out more.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek