Database Articles
  Home arrow Database Articles arrow Page 2 - 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 - Dividing the Data


    (Page 2 of 4 )

    Before we can do anything we need to know 3 important things:

  • How many news items (or whatever) do we want to display?
  • What page are we currently viewing?
  • Based on the answers of the above two questions, where do we begin displaying content?

    Each question can be answered individually with a single line of code.

    <?php
    $display 
    5;
    $pg = (isset($_REQUEST['pg']) &amp;&ampctype_digit($_REQUEST['pg'])) ?
      
    $_REQUEST['pg'] : 1;
    $start $display $pg $display;
    ?>

    $display is given the value of the maximum amount of news you want to display. This can be virtually any number. $pg first checks to see if we have a _REQUEST variable named "pg" available in the query string, and checks whether it is a valid number with the ctype_digit() function; if it is set and a valid number then we keep that value, otherwise we give it the value 1. The reason we perform the check to make sure it is a valid number, is because the query string might be altered by the visitor and therefore we can't trust it.

    The $start variable although simple, takes bit of a further explanation. Whatever news we decide to display will be stored in an array and the value of $start will be the key to let us know what part of the array we should start at (depending what page we're on). This value needs to change dynamically. If we're on page 1, then the value of $start will result in 0 because 5 x 1 - 5 = 0 (that is: $display * $pg - $display). Otherwise, if we're on page 2 then the value of $start will be 5, giving us the "start" of the next 5 items, and so on.

    To get the array of news we want to display varies slightly depending on whether you're using a flat file or a database, but it's the same concept.

    <?php
    /* paginating from a database (not including connection) */
    $result mysql_query("SELECT count(*) FROM news_table");
    $total mysql_result($result0);
    $news mysql_query("SELECT * FROM news_table ORDER BY date_field
    ASC LIMIT $start, $display"
    );

    /* paginating from a flat file */
    $data file('news.txt');
    $total count($data);
    $news array_slice(array_reverse($data), $start$display);
    ?>

    In both versions, we get the total number of news items ($total) which will be used later. The database version collects only the news items we want to display using MySQL's LIMIT. The flat file version is almost identical, but uses different functions. The array_slice() acts similar to LIMIT giving us only the key elements that we want to display.

    In the examples above, with MySQL we sort the new items by a date_field and specify ASC (ascending, or newest items first); whereas with the flat file we simply reverse the data since the newest items are usually appended to the end of the file. You can sort your data any way that you prefer.

    Ultimately, to apply pagination to any given page we will be adding roughly seven lines of code (depending if/how you handle your MySQL connection). The 7th line will be a call to the paginate() function, discussed in the next section. Here is where we're at so far:

    <?php
    $display 
    5;
    $pg = (isset($_REQUEST['pg']) &amp;&ampctype_digit($_REQUEST['pg'])) ?
      
    $_REQUEST['pg'] : 1;
    $start $display $pg $display;

    $data file('news.txt');
    $total count($data);
    $news array_slice(array_reverse($data), $start$display);

    /* will draw the page links */
    // paginate($display, $pg, $total);
    ?>

    How you display your news is entirely up to you and shouldn't effect the actual pagination in any way. For testing purposes, you might try something like:

    <?php
    foreach($news as $value) {
        echo 
    $value.'&lt;hr /&gt;';
    }

    // or...

    while($row mysql_fetch_assoc($news)) {
       echo 
    $row['title'].'&lt;br /&gt;';
       echo 
    $row['body'].'&lt;hr /&gt;';
    }
    ?>

    With the exception of the paginate() function, the code should be perfectly functional at this point in that changing the query string variable "pg" (ie: script.php?pg=3) will only display the appropriate news items that you want for that specific page. The next thing to do is create the navigation.

    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 4 Hosted by Hostway
    Stay green...Green IT