Free Webmasters Resources, Webmaster Tools, CGI Scripts, Web Tutorials, Web Articles  

Dating Services Software
Navigation
Home
Advertise
Webmaster Forums
Webmaster Resources
Webmaster Tools
Web Articles
Tutorials
Free Newsletter

Link To Us
Site Map
About Us
Search
Contact

Webmaster Resources
Resource Home
Submit Resource

Commerce
Free Services
Learning
Online Tools
Our Webmaster Tools
Promotion
Scripts
Software
Web Hosting
Website Design


Webmaster Tools
SEO Tools

- Meta Tag Generator
- Google Position Checker
- Keyword Density Analyzer
- Keyword Generator
- Link Popularity Checker
- Meta Tag Analyzer
- Search Index Report
- Search Engine Simulator
- Web Page Analyzer
- Yahoo Position Checker

HTML Tools

- Color Chart
- Color Converter
- Dreamweaver Code Cleaner
- Frontpage Code Cleaner
- HTML Entities List
- Web Page Size Checker

Domain Tools

- Domain Typo Generator
- Whois Lookup

Other Tools

- HTTP Headers Viewer
- IP Address


Free Newsletter
This is a private mailing list and will never be sold or given away for any reason. Newsletter sent out in HTML format.


First Name:


Email Address:




More Information
Archives


Poll
Where do you live?
Africa
North America
South America
Asia
Australia/Pacific
Europe
 

Results
Past Results


Site Search
Search this entire Website for articles, tutorials, reviews and other resources.


Adv. Search

Scripts & Programs
Submit Script

ASP Scripts
C and C++ Scripts
JavaScripts
Perl and CGI Scripts
PHP Scripts
Remotely Hosted


Web Articles
Submit Article

Coding
eZine Production
Marketing Tips
Online Business
Other Internet Articles
Search Engines
Web Design
Web Hosting


Tutorials
Submit Tutorial

ASP Tutorials
C and C++ Tutorials
ColdFusion Tutorials
DHTML Tutorials
Flash Tutorials
Graphic Tutorials
HTML Tutorials
JavaScript Tutorials
Perl Tutorials
PHP Tutorials
Web Design Tutorials
XML Tutorials


Showing MySQL Results In Pages

Home Page :: Authors Login :: Author Signup :: Search :: More Articles

Home :: PHP/MySQL


About This Author :: Request Reprint :: Print Article :: Tell A Friend


Showing MySQL Results In Pages
By Ben Sinclair
Contact Ben Sinclair


100 results on one page takes too long to load and can also look messy. Lets get started on putting your MySQL Database results on sepearte pages with PHP. I am assuming you have atleast a basic knowledge of PHP and MySQL because this is what you will need to know to understand this tutorial.

Generating The Pages

First of all you need to find out how many links are in the database and then place them in pages Here's how:


<?php
// ==============
// Configuration
// ==============
$perpage = "10"; // How many results to show per page
$pageshow = "10"; // How many pages you want to show in the direction bar

$records = mysql_fetch_array(mysql_query("select count(*) as results from database_table"));
$page_num = ceil($records[results] / $perpage);
$page = ($page) ? $page : 1;
$vstart = $perpage * ($page-1);
$page_start = floor(($page-1)/ $pageshow ) * $pageshow ;
$page_end = $page_start + $pageshow;
for (
$p=$page_start+1 ; ($p <= $page_end) && ($p <= $page_num)&nbsp; ; $p++ )
{
if (
$page == $p) {
$direct_bar .= "<b>$p</b>�";
} else {
$direct_bar .= "<a href='$PHP_SELF?page=$p'>$p</a>�";
}
}
if (
$records[results] > $vstart+$perpage ) {
$next_p=$page+1;
$next_list = "<a href='PHP_SELF?page=$next_p'>Next >></a>�\n";
}
if (
$page>1) {
$prev_p=$page-1;
$prev_list="<a href='PHP_SELF?page=$prev_p'><< Prev</a>\n";
}
?>



Connecting To The MySQL Database

Now that the script knows how many records and pages it has to create, you now retreive your information from the database:


<?php
$query
= "select * from database_table";
$query_result = mysql_query($query) or die("<b>MySQL Error:</b> " . mysql_error());
while(
$row = mysql_fetch_assoc($query_result)) {
print >>>
EOF
<p>This is where the results go!</p>
EOF;
}
?>



Putting It All Together

OK, now you have the two main parts of the script, you will need to put it all together including a couple other little pieces of code:


<?php
// ==============
// Configuration
// ==============
$perpage = "10"; // How many results to show per page
$pageshow = "10"; // How many pages you want to show in the direction bar

$records = mysql_fetch_array(mysql_query("select count(*) as results from database_table"));
$page_num = ceil($records[results] / $perpage);
$page = ($page) ? $page : 1;
$vstart = $perpage * ($page-1);
$page_start = floor(($page-1)/ $pageshow ) * $pageshow ;
$page_end = $page_start + $pageshow;
for (
$p=$page_start+1 ; ($p <= $page_end) && ($p <= $page_num)&nbsp; ; $p++ )
{
if (
$page == $p) {
$direct_bar .= "<b>$p</b>�";
} else {
$direct_bar .= "<a href='$PHP_SELF?page=$p'>$p</a>�";
}
}
if (
$records[results] > $vstart+$perpage ) {
$next_p=$page+1;
$next_list = "<a href='PHP_SELF?page=$next_p'>Next >></a>�\n";
}
if (
$page>1) {
$prev_p=$page-1;
$prev_list="<a href='PHP_SELF?page=$prev_p'><< Prev</a>\n";
}

// Below will show the page numbers
print >>>EOF
Pages
: $prev_list : $direct_bar : $next_list
EOF
;

$query = "select * from database_table limit $vstart,$perpage";
$query_result = mysql_query($query) or die("<b>MySQL Error:</b> " . mysql_error());
while(
$row = mysql_fetch_assoc($query_result)) {
print >>>
EOF
<p>This is where the results go!</p>
EOF;
}

// Below will show the page numbers
print >>>EOF
Pages
: $prev_list : $direct_bar : $next_list
EOF
;
?>



That's it! Hope this helps you with your page problems!

Enjoy!

Discuss this in our Webmaster Forums

About The Author:
Ben Sinclair

Ben Sinclair is the Webmaster of Webmaster Resources 101(/)

Go To Top

This article has been read 10 times.


About This Author :: Request Reprint :: Print Article :: Tell A Friend



Advertising
Advertise your website here. View Advertising Information.

Google Ranking Tool
Search Engine OptimizationGet This Tool Free!

Home   Search   Contact   About Us  

Hot Resources
We have found some of the Hottest Resources around just for you! Check these out:
Search Engine Promotion
- Search engine specialist
- Over 200 clients worldwide
- SEO as low as $499
- Link Building for $150

More info...

Online Dating Script

aeDating 3.0 new version of famous dating software with free installation, lifetime upgrades and technical support. New templates, private photos, credits payment system, integrated with phpBB, banner system, new IM.

Visit resource

Search Engine Placement
Since 1997 StepForth has been a result and client oriented search engine optimization and placement company. Our SEO services are guaranteed and proven. Questions? Contact us: 1-877-385-5526

More info...

Best Web Hosting!
- LunarPages ($7.95)
- iPowerWeb ($7.95)
- Globat ($7.50)
- PowWeb ($7.77)

WebHosting $7.50/mo
$7.50/mo - 1000 MB Space, 40 GB Transfer, 24/7 Support, 99.9% Uptime, FREE Domain and FREE Setup.

More info...


Sponsors
Web Hosting
Web Hosting India
StartLogic
Web Hosting
Cheap Web Hosting

Search Engines
Search Engine Optimization SEO
NetVisits Web Directory
Search Engine Optimization

Webmaster Tools
Webmaster Resources

Software
MAS 90

Domain Names
Resell Domain Names and More!

Link Popularity
Reciprocal Link Exchange Directory
Link Building Campaigns

Great Deals
Cheap Flights
Cheap Car Rental
Cheap Mobile Phones
Plasma Televisions



Partners
Dreamweaver Web Templates
Motivational Memo
Website Advertising
Php Scripts
Cheap Web Hosting
Cheap Web Host
Free Webmaster Resources
Need CGI
Desro Webmaster Resources
Web-Mastery Web-Resources
Top 10 Hosts - PHP MySQL

Books
Perl Cookbook

Perl Cookbook


Recommended Web Host

Contact Information

Powered by Outasight Enterprises Pty Ltd
PO Box 1218 Mudgeeraba QLD 4213 AUS
All content � Webmaster Resources 101, 2002-2004 unless otherwise stated.
Design by OE Design Hosted by Surf Speedy View our Privacy Policy
Page loaded in 2.098 secs.
6 users online.

The Hottest Reseller Program Going!