i am trying to create a pagination in which there are 5 items at a time depending on the number of items in DB. i wrote this code . but i dono how to go further .its buggy..any better pagination or alteration for this
<?php
$myresult .= "<div class='pagination' >";
if ($pagenum == 1)
{
}
else
{
$pagenum = 1;
$myresult .= "<a href='javascript:newPage(\"".$pagenum."\")'> first </a>";
$myresult .= " ";
$previous = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous."\")'> Prev </a>";
if ($pagenum == $last)
{
$previous3 = $pagenum-4;
$myresult .= "<a href='javascript:newPage(\"".$previous3."\")'> $previous3 </a>";
$previous2 = $pagenum-3;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
}
if ($pagenum > 2)
{
$previous1 = $pagenum-2;
$myresult .= "<a href='javascript:newPage(\"".$previous1."\")'> $previous1 </a>";
}
if ($pagenum > 1)
{
$previous2 = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> $previous2 </a>";
$myresult .= " ";
}
}
$myresult .= "<span class=\"disabled\"> $pagenum开发者_JS百科 </span>";
if ($pagenum == $last)
{
}
else {
if($pagenum < $last - 1)
{
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> $next </a>";
}
if($pagenum < $last - 2)
{
$next1 = $pagenum+2;
$myresult .= "<a href='javascript:newPage(\"".$next1."\")'> $next1 </a>";
}
if($pagenum == 1 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
$next3 = $pagenum+4;
$myresult .= "<a href='javascript:newPage(\"".$next3."\")'> $next3 </a>";
}
if($pagenum == 2 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> $next2 </a>";
}
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'> Next </a>";
$myresult .= "<a href='javascript:newPage(\"".$last."\")'> Last</a>";
}
$myresult .= "</div>";
$myresult .= "</br>";
?>
Maybe not the best answer ever but here is something I have used:
<?php
class Pagination {
function __construct() {
}
function getPaginatinationNavigation($page = 1, $num_rows, $rows_per_page, $page_name)
{
$lastpage = ceil($num_rows/$rows_per_page);
$page = (int)$page;
if ($page > $lastpage)
{
$page = $lastpage;
}
if ($page < 1) {
$page = 1;
}
$content='<p style="text-align: center;">';
if ($page == 1) {
$content.= " FIRST PREV ";
} else {
$content.= " <a href='$page_name?page=1'>FIRST</a> ";
$prevpage = $page-1;
$content.= " <a href='$page_name?page=$prevpage'>PREV</a> ";
}
$content.= " ( Page $page of $lastpage ) ";
if ($page == $lastpage) {
$content.= " NEXT LAST ";
} else {
$nextpage = $page+1;
$content.= " <a href='$page_name?page=$nextpage'>NEXT</a> ";
$content.= " <a href='$page_name?page=$lastpage'>LAST</a> ";
}
$content.= '</p>';
return $content;
}
}
?>
Import the Object
require_once('classes/Pagination.php');
Get the amount of total rows:
$query= "SELECT COUNT(*) FROM TABLE_NAME";
$row = mysql_fetch_array($getResults);
$numRecords = $row[0];
Make the LIMIT to limit the rows based upon page number:
$limit = ' LIMIT ' . ($page - 1) * 25 .', 25';
Query USING LIMIT
$query = 'SELECT * FROM TABLE_NAME' . $limit;
$getResults=mysql_query($query) or die(mysql_error());
Display the Results as you normally would:
while($row = mysql_fetch_array($getResults))
{
DISPLAY RESULTS HERE
}
Use the Class to spit out Navigation:
$pagination->getPaginatinationNavigation($page, $numRecords, 25, 'display_page_name.php');
So Overall, for pagination you need to make 2 queries:
1) To get the total amount of records in your search 2) To get the Records LIMITed to those items in a range: say 25 to 50 or 1000 to 1050
You display records from the limited query and to go to the next set of records you increase the page number by one.
Hope this helps. Don't forget to scrub any data you get from the url querystring.
Let me know if you would like me to explain further.
精彩评论