Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 4th, 2004, 05:48 AM
Authorized User
Join Date: Sep 2003
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default recordset navigation bar...

how can create a recordset navigation bar goolge style (1 2 3 4 5 ..next)? there is a tutorial that explain this?

thank you :)


Reply With Quote
  #2 (permalink)  
Old March 4th, 2004, 01:22 PM
Friend of Wrox
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts

No idea. :)

However, I did some searching, and found this:


Maybe this will help you...?

Reply With Quote
  #3 (permalink)  
Old March 4th, 2004, 05:49 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

You can use the LIMIT and OFFSET parts of a query to select subsets of an entire result set. Keep in mind that a database is just a set of data -- there's no guarantee of any sort order unless you specify one in your query.

As such, you can figure out the total number of rows in the result set:

$count = query("SELECT COUNT(*) FROM ....");

Dividing this number by the number of records per page will give you the number of pages you need:

$num_pages = ceil($count / $rows_per_page);

We use the ceil() function because you'll need a new page for the last few rows. For example, if you have 25 rows, and display 10 rows per page, 2.5 doesn't make sense for "num_pages", but 3 does.

Now, you can select subsets of the data based on what page you're currently viewing. Assume the page number starts with 1. (In our 25 rows example, we'll have pages 1, 2, and 3). We use the page number and rows_per_page to calculate the offset for our query:

$offset = ($page_number - 1) * $rows_per_page;

We use ($page_number - 1) because the first page displays rows 0 - 9, not rows 1 - 10.

$result = query("SELECT * FROM table ORDER BY x LIMIT $rows_per_page OFFSET $offset");

Hope this gives you an idea of how to do this.

Generating page number links is easy -- once you know how many pages of results there are, you just generate a link for each one:

$links = array();
for ($i = 1; $i <= $num_pages; ++$i)
    if ($page_number !== $i) // where $page_number is the currently viewed page
        $links[] = "<a href=\"{$_SERVER['PHP_SELF']}?page_number={$i}\">{$i}</a>";
        $links[] = "{$i}";

echo join(" | ", $links);

Next and Previous links are easy, too.

if ($i == 1)
   $prev = "Prev"; // first page, no previous page. Don't create link.
   $prev = "<a href=\"{$_SERVER['PHP_SELF']}?page_number=" . ($i - 1) . "\">Prev</a>";

if ($i == $num_pages)
    $next = "Next"; // last page, no next page. Don't create link.
   $next = "<a href=\"{$_SERVER['PHP_SELF']}?page_number=" . ($i + 1) . "\">Next</a>";

echo "{$prev} | {$next}";

Take care,

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
navigation bar problem Aritra ASP.NET 1.0 and 1.1 Basics 5 March 13th, 2007 12:32 PM
page with navigation bar problem zouky ASP.NET 1.0 and 1.1 Basics 0 October 21st, 2004 03:03 AM
navigation bar in templates amc Dreamweaver (all versions) 1 July 9th, 2004 03:48 AM
Navigation Bar help Varg_88 Classic ASP Databases 1 May 12th, 2004 07:33 AM

All times are GMT -4. The time now is 11:16 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.