php / mysql pagination
I have a table w开发者_运维问答ith 58 records in mysql database. I was able to connect to my database and retrive all records and made 5 pages with links to view each pages using php script.
webpage will look like this:
name number
john 1232343456
tony 9878768544
jack 3454562345
joe 1232343456
jane 2343454567
andy 2344560987
marcy 9873459876
sean 8374623534
mark 9898787675
nancy 8374650493
1 2 3 4 5
that's the first page of 58 records and those 5 numbers at bottom are links to each page that will display next 10 records. I got all that. but what I want to do is display the links in this way:
1-10 11-20 21-30 31-40 41-50 51-58
note: since i have 58 records, last link will display upto 58, instead of 60.
Since I used the loop to create this link, depending on how many records i have, the link will change according to the number of records in my table. How can i do this?
Thanks.
EDIT: since first page is already displaying, 1-10 will not be a link. same goes for other pages. whatever page is displaying, that link will not be a link.
EDIT2: this is my code for those links
$limit=10;
if($totalrecords > $limit )
{
echo "<table align = 'center' width='50%'><tr>";
// Display the page links at center. Current page will not be a link.
echo "<td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $totalrecords;$i=$i+$limit)
{
if($i <> $current)
{
echo " <a href='$page_name?start=$i'>$l</a> ";
}
else
{
echo "$l";
}
$l=$l+1; // Current page is not displayed as link.
}
echo "</td></tr></table>";
}
in this code, each page links display as:
1 2 3 4 5
Something like this should help:
<?php
$results_per_page = 10;
$results_count = 58;
foreach(
array_chunk(
(array)range(1, $results_count),
$results_per_page
)
as $page_number => $results
){
$begin = array_slice($results, 0, 1);
$end = array_slice($results, -1, 1);
echo "<a href=\"?page={$page_number}\">",
$begin[0],"-",$end[0],
"</a>\n";
}
?>
Output
<a href="?page=0">1-10</a>
<a href="?page=1">11-20</a>
<a href="?page=2">21-30</a>
<a href="?page=3">31-40</a>
<a href="?page=4">41-50</a>
<a href="?page=5">51-58</a>
Well, the code really depends on something that you already have, but the algorithm would be something like this:
- Get the count of the total records
- Divide the total records amount by the pagination size (in this case is 10)
- Use the remainder (or module) so the last would appear as the last page.
Would be better if you post some code.
The links would be something like this
<a href="?page=5">50-58</a>
I think I am missing the point on the question.
精彩评论