Database Articles

  Home arrow Database Articles arrow Page 4 - 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 - 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.
    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 7 - Follow our Sitemap