Good Evening,
I am currently writing a script to search a MSSQL database. Everything is working OK, to the point I get:
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$Page_End = $Per_Page * $Page;
if ($Page_End > $Num_Rows)
{
$Page_End = $Num_Rows;
}
When I get to the part below
if ($Page_End > $Num_Rows)
{
$Page_End = $Num_Rows;
}
it’s like something errors out - on my page, it shows everything from $Num_Rows) on.
Any ideas
I just changed a few things, mainly the order of when page is defined and set defaults to 1 if page=0 or over the limit.
<?php
if($Num_Rows<=$Per_Page){
$Num_Pages = 1;
}elseif(($Num_Rows % $Per_Page)===0){
$Num_Pages = (int)($Num_Rows/$Per_Page);
}else{
$Num_Pages = (int)($Num_Rows/$Per_Page)+1;
}
$Page = (!empty($_GET['Page']) && $_GET['Page'] <= $Num_Pages ? $_GET['Page'] : 1);
$Page_Start = (($Per_Page*$Page)-$Per_Page);
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_End = ($Per_Page * $Page > $Num_Rows ? $Num_Rows : $Per_Page * $Page);
?>
The whole block:
if($Num_Rows<=$Per_Page){
$Num_Pages = 1;
}elseif(($Num_Rows % $Per_Page)===0){
$Num_Pages = (int)($Num_Rows/$Per_Page);
}else{
$Num_Pages = (int)($Num_Rows/$Per_Page)+1;
}
can be replaced with single line:
$Num_Pages = ceil($Num_Rows/$Per_Page);
system
Closed
4
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.