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;\">« </a>";
if($i == $PAGE) #Doesn't print a link itself, just prints page number
echo "<font color=\"#ff3333\"> $i </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;\"> $i </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;\"> »</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
developerWorks - FREE Tools! |
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
|
|
|
|
As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |