|
-
Sep 7th, 2005, 08:43 PM
#1
Thread Starter
Addicted Member
<<Previous 1,2,3,4,5,6 Next>>
How will i do this? is this possible using PHP & MYSQL?
like if i have 100 records u just want to show the first 10 then have a link like <<Previous 1,2,3,4,5,6 Next>> to navigate through the records..
Thanks in advance.. ^^
-
Sep 7th, 2005, 09:17 PM
#2
Re: <<Previous 1,2,3,4,5,6 Next>>
This may not be the best way to do it but if you have an ID field for example you can select records based on that field.
PHP Code:
select * from YourTable WHERE ID between 1 and 10
-
Sep 7th, 2005, 09:18 PM
#3
<?="Moderator"?>
Re: <<Previous 1,2,3,4,5,6 Next>>
This is what i use
PHP Code:
function Nav_Bar($page,$pages,$start_page,$count_page)
{
$Nav_Bar = "Pages($page of $pages) [";
if($pages > 1)
{
if($page > 1)
{
$prev = $page - 1;
$Nav_Bar .= "<a href=\"{$_SERVER['PHP_SELF']}?page=1\" style=\"font-weight:bold;\"> First </a> <a href=\"{$_SERVER['PHP_SELF']}?page=$prev\" style=\"font-weight:bold;\"> < </a>";
}
if($page>1)
{
$start_page = ($page - 3);
}
$start_page = $page <= 3 ? 1 : $start_page;
$start_page = $page == $pages ? $pages - 5 : $start_page;
$start_page = $start_page < 0 ? 1 : $start_page;
for($count_page = $start_page;$count_page <= $pages; $count_page++)
{
if($count_page > ($start_page + 6) || $count_page == 0)
{
break;
}
if($count_page == $page)
{
$Nav_Bar .= " <b>$count_page</b> ";
}
else
{
$Nav_Bar .= "<a href=\"{$_SERVER['PHP_SELF']}?page=$count_page\"> $count_page </a>";
}
}
if($page < $pages)
{
$next = $page + 1;
$Nav_Bar .= "<a href=\"{$_SERVER['PHP_SELF']}?page=$next\" style=\"font-weight:bold;\"> > </a> <a href=\"{$_SERVER['PHP_SELF']}?page=$pages\" style=\"font-weight:bold;\"> Last </a>";
}
}
else
{
$Nav_Bar .= "<b>$page</b>";
}
$Nav_Bar .= "]";
return $Nav_Bar;
}
And heres an example of using it by selecting values from a MySQL table called photos
PHP Code:
$page = 1;
$per_page = 10;
if(isset($_REQUEST['page'])){
if($_REQUEST['page']!='' || is_numeric($_REQUEST['page'])){
$page = $_REQUEST['page'];
}
}
$sql_max = "SELECT * FROM photos";
$max_results = mysql_query($sql_max);
$max_rows = mysql_numrows($max_results);
$pages = ceil($max_rows / $per_page);
$offset = ($page - 1) * $per_page;
$query = "SELECT * FROM photos ORDER BY ID ASC LIMIT $offset, $per_page";
$result = mysql_query($query);
$Nav_Bar = Nav_Bar($page,$pages,$start_page,$count_page,$gallery);
-
Sep 7th, 2005, 09:56 PM
#4
Thread Starter
Addicted Member
-
Sep 7th, 2005, 10:16 PM
#5
Re: <<Previous 1,2,3,4,5,6 Next>>
Nice code John....i might use it for a script i'll be working on soon
-
Sep 7th, 2005, 10:56 PM
#6
Re: <<Previous 1,2,3,4,5,6 Next>>
Its called pagination - the Hobo posted a function a while back which does this very well and I've linked to it in the FAQ:
http://www.vbforums.com/showthread.p...51#post1988751
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|