Below is my paging code it works great on my site everywhere because I can just include this file where I want paging and all my mysql statement return a value for
$reccnt = number of mysql results
$pagesize = how many items to show per page
I am having 1 problem though, I do not understand all the code as I did not write it all, some times the value for
$last is wrong, can someone who understands the code better help me to get the correct math done to get the right last page value everytime, most of the time it returns the correct result but sometimes depeneding on the number of mysql = reccnt results, it will make the last page value = the value that goes to page 1 instead
Here is an example on a page that had 73 results the paging for last page did not work, one a new entry was added to make it 74 items, the last result worked perfect
$reccnt = 73;
$pagesize = 9;
<?php
// Paging Functions
function qry_str($arr, $skip = '') {
$s = "?";
$i = 0;
foreach ($arr as $key => $value) {
if ($key != $skip) {
if (is_array($value)) {
foreach ($value as $value2) {
if ($i == 0) {
$s .= "$key%5B%5D=$value2";
$i = 1;
} else {
$s .= "&$key%5B%5D=$value2";
}
}
} else {
if ($i == 0) {
$s .= "$key=$value";
$i = 1;
} else {
$s .= "&$key=$value";
}
}
}
}
return $s;
}
function rounding($no, $direction) { //Round number up for paging files
$skip = 0;
if (is_float($no) and $direction = 1) {
$exploded = explode(".", $no);
$nrr = $exploded[0] + 1;
$skip = 1;
}
if (is_float($no) and $direction = 0) {
$exploded = explode(".", $no);
$nrr = $exploded[0];
$skip = 1;
}
if (!is_float($no) and $skip == 1) {
$nrr = $nrr;
} else {
$nrr = floor($nrr);
}
return $nrr;
}
//if results are greater then amount that we allow on 1 page, we make paging
if ($reccnt > $pagesize) {
$start = (!empty($_GET['start'])) ? $_GET['start'] : 0;
//get number of pages = results dived by what we allow on 1 page
$num_pages = $reccnt / $pagesize;
$qry_str = $_SERVER['argv'][0];
$m = $_GET;
$qry_str = qry_str($m);
$j = $start / $pagesize - 5;
if ($j < 0) {
$j = 0;
}
$k = $j + 9;
if ($k > $num_pages) {
$k = $num_pages;
}
$j = intval($j);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="paginator">
<tr>
<td width="" align="center" height="20" class="black11bold">
<?PHP if($start!=0){?>
<a href="<?PHP echo $SITE_PATH;?><?PHP echo $qry_str;?>&start=0" class="bluelinkbold" >« First</a>
<a href="<?PHP echo $SITE_PATH;?><?PHP echo $qry_str;?>&start=<?PHP echo $start-$pagesize;?>" class="bluelinkbold" >« Previous</a> ...
<?PHP }?>
<?PHP
for($i=$j;$i<$k;$i++){
if($i==$j)echo "";
if(($pagesize*($i))!=$start){
?>
<SPAN style="FONT: bold 15px Arial, Helvetica, sans-serif; COLOR: #00427e">
<a href="<?=$SITE_PATH?><?=$qry_str?>&start=<?=$pagesize*($i)?>" style="text-decoration:none;" class="bluelinkbold">
<?=$i+1?>
</a></span>
<?PHP
}else{
?>
<b> <?=$i+1?> </b>
<?PHP
} //end if
} //end for loop
?>
<?PHP
if($start+$pagesize < $reccnt){
$roundup = '1';
$last = $reccnt/$pagesize;
$last = rounding($last,$roundup);
//$last = round($reccnt/$pagesize);
$last = $last*$pagesize-$pagesize;
$last = str_replace("-", "", $last);
?>
... <a href="<?=$SITE_PATH?><?=$qry_str?>&start=<?=$start+$pagesize?>" class="bluelinkbold">Next
»</a>
<a href="<?=$SITE_PATH?><?=$qry_str?>&start=<?=$last?>" class="bluelinkbold">Last
»</a>
<?PHP
}
?>
</td>
</tr>
</table>
<?PHP
}
?>