-
Apr 21st, 2005, 03:20 AM
#1
Thread Starter
Hyperactive Member
How to create Back / Next Links - Resolved
Hi,
I have a big set of records and i want to have Back / Next thing in there with page numbers for example:
If i am on page 10 then it should show up like:
Back 7 8 9 (10 disabled) 11 12 13 Next
How can i do this please help very urgent.
Thanks
Last edited by AvisSoft; Apr 27th, 2005 at 04:17 AM.
Reason: Marking it as resolved.
Tapan Bhanot,
CEO, Avis Software.
Website: www.avissoftware.com
-
Apr 21st, 2005, 04:58 AM
#2
Re: How to create Back / Next Links
Its referred to as link pagination and there is a thread about it here which has a useful function to generate the links.
Last edited by visualAd; Apr 21st, 2005 at 06:31 AM.
-
Apr 21st, 2005, 06:29 AM
#3
Thread Starter
Hyperactive Member
Re: How to create Back / Next Links
Hi,
Uhm...sorry but it took me to a visual basic source page which has something discussing about loops. I need to do this in PHP with MySQL records.
Thanks
Tapan Bhanot,
CEO, Avis Software.
Website: www.avissoftware.com
-
Apr 21st, 2005, 06:32 AM
#4
Re: How to create Back / Next Links
Sorry - I'm not sure how i managed that - but I've corrected the link now.
-
Apr 21st, 2005, 06:57 AM
#5
Thread Starter
Hyperactive Member
Re: How to create Back / Next Links
Hi,
Thanks for the link, i think this is just what i needed but i am still not sure how to use that function ..please tell me how can i use it ?
Thanks
Tapan Bhanot,
CEO, Avis Software.
Website: www.avissoftware.com
-
Apr 21st, 2005, 08:19 AM
#6
Re: How to create Back / Next Links
The function there deals with generation of the links. You will need to supply it with the following information to generate the HTML links.
- Total items altogether.
- Items to display per page.
- The current page.
- The link used to generate the page.
I have written a small script which demonstrates how to use the function in conjunction with a database. You can see the example script here:
http://adam.codedv.com/examples/pagination.php
This is the page source code:
PHP Code:
<?php
mysql_connect('host', 'username', 'password');
/* get the total items to display per page */
if (!isset($_GET['pp'])) {
$pp = 10;
} else {
$pp = intval($_GET['pp']);
}
/* get the page number to display */
if (!isset($_GET['p'])) {
$p = 1;
} else {
$p = intval($_GET['p']);
}
/* find the start record */
$start = ($p * $pp) - $pp;
/* build the query */
$query = "SELECT SQL_CALC_FOUND_ROWS txt FROM items LIMIT $start,$pp";
if (! $result = mysql_query($query)) {
echo($db->error());
die('Query error.');
}
/* find the total number of records */
$query = 'SELECT FOUND_ROWS()';
if (! $found = mysql_query($query)) {
echo($db->error());
die('Query error.');
}
$row = mysql_fetch_row($found);
$total_rows = $row[0];
?>
<html>
<head>
<title>Pagination Example</title>
</head>
<body>
<table>
<?php while ($row = mysql_fetch_row($result)): // print all the rows ?>
<tr>
<td><?php echo($row[0]); ?></td>
</tr>
<?php endwhile; ?>
</table>
<p><?php echo(createlinks($total_rows, $pp, $p, $_SERVER['PHP_SELF'] . "?pp=$pp&p=")) // print out the links ?></p>
</body>
</html>
<?php
function createlinks($totalItems, $perPage, $currentPage, $link) {
// First, figure out the number of pages there will be total:
$totalPages = floor($totalItems / $perPage);
$totalPages += ($totalItems % $perPage != 0) ? 1 : 0;
// Start building page links:
if ($totalPages > 1) {
$output = 'Pages [' . $totalPages . '] ';
$loopStart = 1; $loopEnd = $totalPages;
if ($totalPages > 5) {
if ($currentPage <= 3) {
$loopStart = 1;
$loopEnd = 5;
} else if ($currentPage >= $totalPages - 2) {
$loopStart = $totalPages - 4;
$loopEnd = $totalPages;
} else {
$loopStart = $currentPage - 2;
$loopEnd = $currentPage + 2;
}
}
// Add the "first page" link:
if ($loopStart != 1) {
$output .= '<a href="' . $link . '1">«</a> ';
}
// Use the variables we setup above to loop the links:
for ($i = $loopStart; $i <= $loopEnd; $i++) {
// If this is our current page:
if ($i == $currentPage) {
// Output bold without a link:
$output .= '<b>' . $i . '</b> ';
// This is not our current page:
} else {
// Output the link:
$output .= '<a href="' . $link . $i . '">' . $i . '</a> ';
}
}
// Add the "last page" link:
if ($loopEnd != $totalPages) {
$output .= '<a href="' . $link . $totalPages .'">»</a> ';
}
return $output;
} else {
return '';
}
}
?>
Last edited by visualAd; Oct 5th, 2005 at 12:54 AM.
-
Apr 27th, 2005, 04:05 AM
#7
Re: How to create Back / Next Links
Has your problem been solved? - If so then please mark this thread as resolved.
-
Apr 27th, 2005, 04:16 AM
#8
Thread Starter
Hyperactive Member
Re: How to create Back / Next Links
Hi,
Thanks for the help but the code you provided did'nt helped..so i had to use old code with some modifications.
Thanks
Tapan Bhanot,
CEO, Avis Software.
Website: www.avissoftware.com
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
|