Database Articles

  Home arrow Database Articles arrow Page 3 - Multiple Pages of Data from a Text Fil...
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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap