PHP
  Home arrow PHP arrow Page 5 - 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 - Extending the use of the "Pager class": more examples to come
    ( Page 5 of 5 )

    Say we want to use HTML lists to display the database records. Our new template file, called "record_template1.htm" would be defined in the following way:

    <ul>
      <li>{data0} {data1} - {data2}</li>
    </ul>

    But, wait a minute! Right now you must think that I'm kidding you. Not really. That's all we need to redefine the rendered output. Now, let's declare a few simple CSS rules:

    li {
      font: normal 12px Arial, Helvetica, sans-serif;
      color: #009;
    }

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

    Finally, let's implement the same PHP code, this time passing the new template file to our pager class:

    <?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,'record_template1.htm');

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

    ?>

    Or we can use the procedural approach, with the following lines:

    <?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,'record_template1.htm');

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

    ?>

    Our new paginated result set, using unordered lists, will be displayed as follows:

    Can you see what a difference changing the template file made? The power and flexibility of the pager class is really remarkable. Want to see more? Fine, let's use the same default record template file defined in the first example, but this time change only the CSS rules. Here are the new CSS styles:

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

    }

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

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

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

    As usual, let's implement the PHP code to instantiate the pager class:

    <?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']);

    ?>

    Alternatively, let's take the procedural road to make the MySQL connection:

    <?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']);

    ?>

    According to the new CSS rules, the revamped visual output would be the following:

    Certainly, the class shows how easy it is to change styles for records and display them in a paginated manner. It's just a matter of changing the template files, and we're in business.

    Having shown all of these examples, I believe that I provided you with a wealth of resources for building paginated Web pages. Feel free to use this samples and build your own template files and CSS styles. If you've been feeling creative lately, let your inspiration fly and find new styles to utilize. The possibilities are endless.

    Conclusion

    We have finished our long examination of paging techniques. From the first article onward, I've demonstrated different methods for paginating web pages in a progressive learning curve. I wound up the process by developing a solid and robust PHP pager class, capable of rendering paged MySQL result sets with facility and flexibility.

    For those developers working in the area of recordset paging, this class can certainly be very useful and highly portable. Personally, I've been using the class in several projects with excellent results, so with minor or no modifications, it might be the next solution for your paging needs. Good luck and happy paging!



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek