Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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 :)

joeore

 
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
Default

No idea. :)

However, I did some searching, and found this:

http://forums.devshed.com/t47606/s.html

Maybe this will help you...?

----------
---Snib---
----------
 
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
Default

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>";
    }
    else
    {
        $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.
}
else
{
   $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.
}
else
{
   $next = "<a href=\"{$_SERVER['PHP_SELF']}?page_number=" . ($i + 1) . "\">Next</a>";
}


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




Take care,

Nik
http://www.bigaction.org/




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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.