Site Navigation Code

  Home arrow Site Navigation Code arrow Page lister
SITE NAVIGATION CODE

Page lister
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Ever wanted to limit the number of returns you get from a MySQL Query? You want to show all of them, but you'd really like to show them spanning 2 or three or 15 pages for example... After sleeping on the bus, i discovered how to do this fairly easily.

    By : nsfmc

    <html>
    <head>
    <title>Page Lister</title>
    <style type="text/css">
    a { text-decoration: none }
    body { color: #000; background-color: #fff}
    </style>
    </head>
    <body bgcolor="#ffffff">
    <?PHP
    $PAGE = (isset($page)) ? $page : 1; #We hope that page, not PAGE is in the querystring,
    #if not we set it here
    $dbhost = "localhost"; # These are fairly self explanatory....
    $dbname = "fillmein"; # You can remove these if you've already connected to a DB...
    $dbuser = "fillmein";
    $dbpass = "fillmein";

    $dc = mysql_connect($dbhost,$dbuser,$dbpass); #Use $dc for debugging.
    mysql_select_db($dbname,$dc); # Connect now, forget about it later.

    $QUERY = "SELECT * FROM table"; ## You MUST change this :)!
    $QRETURN= @mysql_query($QUERY);
    $QNUM = @mysql_num_rows($QRETURN);

    $DISPPAGE = 10; #DISPPAGE is the number of items displayed per page.
    $NUMPAGES = ceil($QNUM / $DISPPAGE); #NUMPAGES to show how many pages you WILL get.

    if($QRETURN): #Sanity check on query, only runs if valid query.
    if($QNUM > 0): #We actually got some rows.
    echo "<div align=\"right\">\n<font face=\"Tahoma, Geneva, sans-serif\" size=\"1\">\n";

    for($i = 1; $i <= $NUMPAGES; $i++): #loop to print << 1 2 3... $NUMPAGES >>
    if($i == 1 && $PAGE > 1) #Prints the << first to goto the previous page (not on page 1)
    echo "<a href=\"$PHP_SELF?page=".($PAGE - 1)."\" onMouseOver=\"status='Go to the Previous Page';return true;\" onMouseOut=\"status=' ';return true;\">&#0171;&nbsp;</a>";
    if($i == $PAGE) #Doesn't print a link itself, just prints page number
    echo "<font color=\"#ff3333\">&nbsp;$i&nbsp;</font>";
    if($i != $PAGE) #Other links that aren't this page go here
    echo "<a href=\"$PHP_SELF?page=$i\" onMouseOver=\"status='Go to Page $i';return true;\" onMouseOut=\"status=' ';return true;\">&nbsp;$i&nbsp;</a>";
    if($i == $NUMPAGES && $PAGE != $NUMPAGES) # Link for next page >> (not on last page)
    echo "<a href=\"$PHP_SELF?page=".($PAGE + 1)."\" onMouseOver=\"status='Go to the Next Page';return true;\" onMouseOut=\"status=' ';return true;\">&nbsp;&#0187</a>";
    endfor;

    echo "</font>\n</div>\n";
    echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">\n";
    $START = ($PAGE - 1) * $DISPPAGE;
    mysql_data_seek($QRETURN,$START); #Moves the pointer to right row
    #This loop will go until you a) reach $DISPPAGE or b) there aren't anymore entries
    for($i = 1; $i <= $DISPPAGE && $ARET = @mysql_fetch_array($QRETURN); $i++):
    $VAR = $ARET["col"]; #******** Here's the bit you should update! ********#
    echo "$VAR\n<br><br>\n\n"; #echoes the field...
    # Alternately, if you feel like numbering. I haven't tested this.
    #echo "<b>".$START + ($i - 1)."</b>: $VAR\n<br><br>\n\n";
    endfor;

    echo "</font>\n";
    endif;

    if($QNUM == 0) #if we get no rows
    echo "<div align=\"center\">The database is empty</div>\n";
    endif;

    if(!$QRETURN): # Bogus Query, or mysqld isn't running...
    echo "<div align=\"center\">\n";
    echo "<font face=\"Arial, Verdana, Helvetica, sans-serif\" color=\"#ff3333\" size=\"3\">\n";
    echo "Either the database is down, or the query was invalid\n";
    echo "</font>\n</div>\n";
    endif;
    ?>
    </body>
    </html>
    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.

    More Site Navigation Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    SITE NAVIGATION CODE ARTICLES

    - Simple Menu System
    - Simply image viewer script
    - Simple File Lister
    - Dynamic Error Pages
    - BSoftEditor
    - Yahoo Status
    - Page numbers
    - PHP Search Navigator 1.0
    - Simple Page Navigation
    - An easy page browser ( prev 6 7 8 9 10 next )
    - AutoIndex PHP Script (Directory Indexer)
    - Bs_HtmlNavigation (Navigation and Sitemap cl...
    - Another Paging with Stage
    - Website Navigation via PHP
    - MySQL Paging Class


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