PHP Pagination with Limit Combo box

I have a page which displays records from MySQL Database, It has a Pagination which works fine, But i wanted it add a combo box to let users choose how many records to show up and that’s where I am having trouble with. Any advice and correction will be highly appreciated.

Below is my code:

if($_POST[‘move’]) // from button name=“archive” move record to Archive
{
// bla… bla… bla…
}

$sql = “SELECT COUNT(*) as TOTALFOUND FROM Mytable”;
$result=mysql_query($sql);
$nr0 = (mysql_result($result,0,“TOTALFOUND”));

@ $rpp; //Records Per Page
@ $cps; //Current Page Starting row number
@ $lps; //Last Page Starting row number
$page_num = 1; //Addition for Page Numbering

if(isset($_GET[‘page’]))
{
$page_num = $_GET[‘page’];
}

if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $nr0)) {
$page = 1; //default
}

if(isset($_POST[‘Show’])){ //from Combo box user selection//
$pageLimit = $_POST[‘recno’]; //from Combo box name for user selection//
} elseif(isset($_GET[‘Limit’])){ //from Pagination recieve limit//
$pageLimit = $_GET[‘Limit’]; //from Pagination recieve limit//
} else{
$pageLimit = “5”;
}

$rpp = $pageLimit;
$numofpages = ceil($nr0/$rpp);

// The offset will retrive the limited number from sql
$offset = ($page_num - 1) * $rpp;

//Actual statement to retrieve data
$sql2 = “SELECT * from my_table LIMIT $offset, $rpp”;
$rs=mysql_query($sql2);
$nr = mysql_num_rows($rs);

// print the link to access each page
$self = “/Inbox.php?”;

// Calculating records numbers to display on top of Page
$cps = (($page_num - 1)*$rpp)+ 1;
if($cps >= $nr0){
$cps = $nr0;
}
$lps1 = $page_num * $rpp;
if($lps1 >= $nr0){
$lps = $nr0;
} else {
$lps = $lps1;
}
?>

/////////// Display Section/////////////////////

<? if($nr0==0){ ?>
Empty


"; <? } else { ?>

///////Option to select the number of records///////////

<? switch ($pageLimit) { case "25": $mm="selected"; break; case "50": $mn="selected"; break; case "100": $mh="selected"; break; case "150": $mj="selected"; break; } echo " 25 50 100 150 "; ?>

<input type=“submit” name=“Show” id=“show” value=“Show”">

<? if($nr!==0){echo "Records $cps - $lps of $nr0";}?>
while ($row=mysql_fetch_array($rs)) 
{ 
echo "<td>".$row['Report_Date']."</td>";

}

// Writing the Pagination at the bottom of the page//////////////////////////////////////////
if ($numofpages > ‘1’ ) {
$range =10;
$range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
$range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
$page_min = $page_num- $range_min;
$page_max = $page_num+ $range_max;

        $page_min = ($page_min < 1) ? 1 : $page_min; 
        $page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max; 
        if ($page_max > $numofpages) { 
            $page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1; 
            $page_max = $numofpages; 
        } 
        $page_min = ($page_min < 1) ? 1 : $page_min; 

        if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) { 
		     $page_pagination .= '<li><a title="First" href="'.$self.'page=1&Limit='.$pageLimit.'">&lt;</a></li> '; 
        } 
        if ($page_num != 1) { 
            $page_pagination .= '<li><a href="'.$self.'page='.($page_num - 1). '&Limit='.$pageLimit.'">...</a></li> '; 
        } 
        for ($i = $page_min;$i <= $page_max;$i++) { 
            if ($i == $page_num) 
            $page_pagination .= '<li><strong>' . $i . '</strong></li> '; 
            else 
			$page_pagination.= '<li><a href="'.$self.'page='.$i.'&Limit='.$pageLimit.'">'.$i.'</a></li>';
        } 
        if ($page_num < $numofpages) { 
            $page_pagination.= '<li><a href="'.$self.'page='.($page_num + 1) . '&Limit='.$pageLimit.'">...</a></li>'; 
        } 
        if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) { 
							
		  $page_pagination .= '<li><a title="Last" href="'.$self.'page='.$numofpages.'&Limit='.$pageLimit.'">&gt;</a></li> '; 
		} 
        $page['PAGINATION'] ='<p>'.$page_pagination.'</p>'; 
   		}
		echo "<div id='paginationContainer'>".$page_pagination."</div>";

?>

Thank you so much!

;D

I got it working after several hours playing around… I made some changes on here

if(isset($_POST[‘Show’])) {
$pageLimit = $_POST[‘recno’];
$page_num = 1;
$page = 1;
} else if(isset($_GET[‘Limit’])) {
$pageLimit = $_GET[‘Limit’];
} else {
$pageLimit = “25”;
}

If a user changes the number of rows from the combo box… it will default back to page number 1 with first record.

I want to know any mistakes on what i am doing,? is there any better way? I am newbie and just playing around.

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service