Database Articles
  Home arrow Database Articles arrow Page 4 - 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 - The Final Code


    (Page 4 of 4 )

    <?php
    function paginate($display$pg$total) {
      
    /* make sure pagination doesn't interfere with other query 
    string variables */
      
    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=';
        
      
    /* find out how many pages we have */
      
    $pages = ($total &lt;= $display) ? ceil($total $display);
        
      
    /* create the links */
      
    $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;'
    ;
       
      
    /* display opening navigation */
      
    echo '&lt;div&gt;&lt;p align="center"&gt;';
      echo (
    $pg &gt1) ? "$first : $prev :" '&amp;#171; : &amp;#139; :';
      
      
    /* limit the number of page links displayed */
      
    $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; ';
        
      
    /* display ending navigation */
      
    echo ($pg &lt$pages) ? ": $next : $last" ': &amp;#155; : &amp;#187;';
      echo 
    '&lt;/p&gt;&lt;/div&gt;';
    }

    /* set pagination variables */
    $display 5;
    $pg = (isset($_REQUEST['pg']) &amp;&ampctype_digit($_REQUEST['pg'])) ?
      
    $_REQUEST['pg'] : 1;
    $start $display $pg $display;

    /* paginating from a database */
    $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 flatfile */
    $data file('news.txt');
    $total count($data);
    $news array_slice(array_reverse($data), $start$display);

    paginate($display$pg$total);

    /* display some $news */

    paginate($display$pg$total);
    ?>

    Yay!

    About the Author

    Hi, I'm notepad.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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