Database Articles
  Home arrow Database Articles arrow Page 3 - Multiple Pages of Data from a Text Fil...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
DATABASE ARTICLES

Multiple Pages of Data from a Text File
By: notepad
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2002-07-02

    Table of Contents:
  • Multiple Pages of Data from a Text File
  • Dividing the Data
  • Navigating the Data
  • The Final Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Multiple Pages of Data from a Text File - Navigating the Data


    (Page 3 of 4 )

    To create the navigation links for your pagination we will make a function called paginate(). The function should be available to every page which requires pagination, but doesn't need to be hard-coded. You could, for example, put the function in a file called "my_functions.php" and then include() it when needed.

    The paginate function will be called with 3 variables that we've already defined in the previous section: $display, $pg and $total. The first thing we want to do inside the function is check whether or not any pre-existing query string variables exist and if so, retain them. For example you might have a news script which divides news items into categories, so you're already passing a catid=2 (which you need to keep). The following will setup our new query string:

    <?php
    if (isset($_SERVER['QUERY_STRING']) &amp;&amptrim($_SERVER['QUERY_STRING'])
      != 
    '') {
      if(
    stristr($_SERVER['QUERY_STRING'], 'pg='))
        
    $query_str '?'.preg_replace('/pg=\d+/''pg='
          
    $_SERVER['QUERY_STRING']);
      else
        
    $query_str '?'.$_SERVER['QUERY_STRING'].'&amp;pg=';
    } else
      
    $query_str '?pg=';
    ?>

    We first check if a pre-existing query string is available and actually has a value. If not, then our query string will simply be: ?pg=. If we do have a pre-existing value however, we then check to see if "pg" is already defined using the stristr() function. If "pg" is defined then we need to replace it (done with the regular expression, or preg_replace() function). We are just replacing it with a blank or empty value because we know that "pg" will always be the last (or only) variable in the query string. If "pg" is not defined (but another query string exists), then we simply append it to whatever value we're working with using the ampersand instead of a question mark: &pg=.

    Now if we have:

    news.php
    news.php?catid=2
    news.php?catid=2&amp;pg=4

    Our $query_str variable will end up with:

    ?pg=
    ?catid=2&amp;pg=
    ?catid=2&amp;pg=

    Now that we've got our query string figured out, we need to know how many total pages of data we have to display. Using our variables from earlier, a little math, and rounding off with the ceil() function we end up with this:

    <?php
    $pages 
    = ($total &lt;= $display) ? ceil($total $display);
    ?>

    We're almost ready to start drawing the links, but before we do I'm going to create some variables for them so they are easier to work with later:

    <?php
    $first 
    '&lt;a href="'.$_SERVER['PHP_SELF'].$query_str.'1"&gt;&amp;#171;
    &lt;/a&gt;'
    ;
    $prev '&lt;a href="'.$_SERVER['PHP_SELF'].$query_str.($pg 1).'"&gt;
    &amp;#139;&lt;/a&gt;'
    ;
    $next '&lt;a href="'.$_SERVER['PHP_SELF'].$query_str.($pg 1).'"&gt;
    &amp;#155;&lt;/a&gt;'
    ;
    $last '&lt;a href="'.$_SERVER['PHP_SELF'].$query_str.$pages.'"&gt;
    &amp;#187;&lt;/a&gt;'
    ;
    ?>

    Nothing complicated here. Each link references the current page using PHP_SELF, the first page is 1, the previous page is pg - 1, next page is pg + 1, and the last page is our total number of pages. The entity references are just prettier versions of the greater-than / less-than symbols.

    Now, finally we can begin our navigation:

    <?php
    echo '&lt;div&gt;&lt;p align="center"&gt;';
    echo (
    $pg &gt1) ? "$first : $prev :" '&amp;#171; : &amp;#139; :';
    ?>

    Above we open with a div tag, and draw the previous and next links only if we're not on the first page (otherwise we don't need them linked). Below is where we actually draw the page numbers. I've hard-coded my version to display a minimum of 5 links (if we're at the beginning or end of the total number of pages), or a maximum of 10 links (if we're somewhere in the middle of the total number of pages).

    <?php
    $begin 
    $pg 4;
    while(
    $begin &lt1)
      
    $begin++;
    $end $pg 4;
    while(
    $end &gt$pages)
      
    $end--;

    for(
    $i=$begin$i&lt;=$end$i++)
      echo (
    $i == $pg) ? ' ['.$i.'] ' ' &lt;a href="'.
        
    $_SERVER['PHP_SELF'].$query_str.$i.'"&gt;'.$i.'&lt;/a&gt; ';
    ?>

    I'm basically just setting a $begin and $end variable ($pg + and - 4, plus the current page = 10 page links). I use the while() loops to make sure I'm not creating links to pages that don't exist. In the last line of code above, I draw links for every page in my range of page numbers, except for the current page which is instead placed inside brackets. Now we can close off the div:

    <?php
    echo ($pg &lt$pages) ? ": $next : $last" ': &amp;#155; : &amp;#187;';
    echo 
    '&lt;/p&gt;&lt;/div&gt;';
    ?>

    The only thing left to do is bundle everything we've covered into a function, and review.

    More Database Articles Articles
    More By notepad


       · what else can we say. this is very nice! thank you very much!
       · This doesn't work for me..It keeps saying unexpected ; >(ARGH!!
       · Replace all the occurences of "&" with "&", "<" with "<" and all ">"...
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT