I just thought i'd post my pagination method:
function paginate($numRows, $maxRows, $pageNum=0, $pageVar="page",
$class="txtLink", $pages_text=" Pages ", $rowsText="Results")
{
global $lang;
$navigation = "";
// get total pages
$totalPages = @ceil($numRows/$maxRows);
// develop query string minus page vars
$queryString = "";
if (!empty($_SERVER['QUERY_STRING']))
{
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param)
{
if (stristr($param, $pageVar) == false)
{
array_push($newParams, $param);
}
}
if (count($newParams) != 0)
{
$queryString = "&" . htmlentities(implode("&", $newParams));
}
}
// get current page
$currentPage = $_SERVER['PHP_SELF'];
// build page navigation
if ($totalPages == 1)
{
$navigation = $numRows." ".$rowsText;
}
elseif ($totalPages> 1)
{
$navigation = $numRows." ".$rowsText." / ".$totalPages.$pages_text;
$upper_limit = $pageNum + 3;
$lower_limit = $pageNum - 3;
if ($pageNum > 0) // Show if not first page
{
if(($pageNum - 2)>0)
{
$first = sprintf("%s?".$pageVar."=%d%s", $currentPage, 0, $queryString);
$navigation .= "<a href='".$first."' class='".$class."'>«</a> ";
}
$prev = sprintf("%s?".$pageVar."=%d%s", $currentPage, max(0, $pageNum - 1), $queryString);
$navigation .= "<a href='".$prev."' class='".$class."'>< Prev</a> ";
} // Show if not first page
// get in between pages
for($i = 0; $i < $totalPages; $i++)
{
$pageNo = $i+1;
if ($i==$pageNum)
{
$navigation .= " <strong>[".$pageNo."]</strong> ";
}
elseif ($i!==$pageNum && $i<$upper_limit && $i>$lower_limit)
{
$noLink = sprintf("%s?".$pageVar."=%d%s", $currentPage, $i, $queryString);
$navigation .= " <a href='".$noLink."' class='".$class."'>".$pageNo."</a> ";
}
elseif (($i - $lower_limit)==0)
{
$navigation .= "…";
}
}
if (($pageNum+1) < $totalPages) // Show if not last page
{
$next = sprintf("%s?".$pageVar."=%d%s", $currentPage, min($totalPages, $pageNum + 1), $queryString);
$navigation .= "<a href='".$next."' class='".$class."'>Next ></a> ";
if(($pageNum + 3)<$totalPages)
{
$last = sprintf("%s?".$pageVar."=%d%s", $currentPage, $totalPages-1, $queryString);
$navigation .= "<a href='".$last."' class='".$class."'>»</a>";
}
} // Show if not last page
} // end if total pages is greater than one
return $navigation;
}
function select($query, $maxRows=0, $pageNum=0, $start=0)
{
$this->query = $query;
// start limit if $maxRows is greater than 0
if ($maxRows>0)
{
$startRow = $pageNum * $maxRows;
$query = sprintf("%s LIMIT %d, %d", $query, $startRow, $maxRows);
}
$result = mysql_query($query, $this->db);
if ($this->error())
die ($this->debug());
$output=false; // Set the output to be bool false.
for ($n=0+$start; $n < mysql_num_rows($result); $n++)
{
$row = mysql_fetch_assoc($result);
$output[$n] = $row;
}
return $output;
} // end select
First, someone gave me this, until I really understood it then I modified it (several times). But, it is really good, it omits everything that's not required and also gives you a count of results for the bottom of your page.
Usage:
$numRows = mysql_num_rows($query); // Where you have already
$maxRows = $rowsPerPage; // eg. $rowsPerPage = 20;
if ($_GET['page'])
{
$pageNum = $_GET['page'];
}
else
{
$pageNum = 0;
}
$results = select($query, $maxRows, $pageNum);
$pageinate = paginate($numRows, $maxRows, $pageNum);
echo $paginate;
foreach($i=0; $i<count(); $++)
{
echo $results[$i]['row_product_name']; // for example
}