Paging and number per page not working together

Hi all,

I had a lot of help from cpradio on paging and it works perfectly and I then moved onto other things and now I’m at the trying to give the user control over how many contracts they see per page.

The problem I’m having is that it works, well I got it working my way I suppose, but when its not working correctly.

Firstly when the page loads up first, the ordering is from current contract to old, and I change the number per page and straight away it changes to old first to new last.

Thats probably the easy bit.

My main concern is when Im using it after using another option which is to use my ‘view by’ option which is a load of different ways to view the contracts, from old to new contract, highest contract to lowest and that sort of thing.

Basically I change the view by and all is good so far but then if I then change the number of contracts to view it by it doesnt keep the view by selction and the ordering of the contracts has changed too.

Here is the code:


$search="";
$searchKeyword="";
$trimmed="";
$rows="";

if(isset($_POST['btnsubmit']))
{
$search=$_POST['Search'];
}
else if(isset($_GET['Search']))
{
$search=$_GET['Search'];
}

if(isset($_POST['btnrows']))
{
$rows=$_POST['Rows'];
}
else if(isset($_GET['Rows']))
{
$rows=$_GET['Rows'];
}

// Get the search variable from URL
if(isset($_GET['q']))
{
  $searchKeyword = @$_GET['q'];
  $trimmed = $searchKeyword;
  } else {
  $trimmed = trim($searchKeyword); //trim whitespace from the stored variable
  $trimmed = preg_replace('~[^a-z]~i', null, $trimmed);
}

<div style="position:relative; width:100%;">
<div style="position:relative; width:55px; float:left">
<span style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#ffffff; font-size:13px">Search:</span>
</div>
<div style="position:relative; width:185px; float:left; height:20px; margin:0px; padding:0px;">
<form name="form2" action="contracts.php" method="get" style="position:relative; height:20px;" >
<input type="text" name="q" ></input>&nbsp;<input type="submit" name="searchButton" class="btn-std" value="Go">
</form>
</div>
&nbsp; > <a href="#" style="font-family:Arial, Helvetica, sans-serif;font-size:14px;color:#FFFFFF" title="Click here for Advanced Search">Advanced Search</a>
</div>
 </td>
<form name="form1" action="contracts.php" method="post" enctype="multipart/form-data">
<td height="27" style="position:relative;left:14px;width:340px;border-right:#000000 dotted 1px;"><span style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#ffffff; font-size:13px;position:relative;">View By:</span>
<select name="Search">
<option value="">Select Order</option>
<!--<option <?php if($cStatus == "Active") echo "selected"; ?> value="Active">Active</option>-->
<option value="AllContractsOld-New">All Contracts (Old-New)</option>
<option value="AllContractsNew-Old">All Contracts (New-Old)</option>
<option value="AllContractsOnHold">All Contracts (On Hold)</option>
<option value="AllContractsActive">All Contracts (Active)</option>
<option value="AllContractsTerminated">All Contracts (Terminated)</option>
<option value="ByCountryA-Z">By Country (A-Z)</option>
<option value="ByCountryZ-A">By Country (Z-A)</option>
<option value="ByRegionA-Z">By Region (A-Z)</option>
<option value="ByRegionZ-A">By Region (Z-A)</option>
<option value="ByNumberModulesO-X">By Number of Modules (O-X)</option>
<option value="ByNumberModulesX-O">By Number of Modules (X-O)</option>
<option value="ByContractValueLow-High">By Contract Value (Low-High)</option>
<option value="ByContractValueHigh-Low">By Contract Value (High-Low)</option>
<option value="ByFoodCheckYes">By FoodCheck (Yes)</option>
<option value="ByPoolCheckYes">By PoolCheck (Yes)</option>
<option value="ByAquaCheckYes">By AquaCheck (Yes)</option>
<option value="ByFireCheckYes">By FireCheck (Yes)</option>
<option value="ByCrisisCheckYes">By CrisisCheck (Yes)</option>
<option value="ByEcoCheckYes">By EcoCheck (Yes)</option>
<option value="BySafetyCheckYes">By SafetyCheck (Yes)</option>
<option value="ByRoomCheckYes">By RoomCheck (Yes)</option>
<option value="ByQualityCheckYes">By QualityCheck (Yes)</option>
<option value="BySupplyCheckYes">By SupplyCheck (Yes)</option>
<option value="ByTourcheckYes">By Tourcheck (Yes)</option>
</select>
<input type="submit" name="btnsubmit" value="Submit" class="btn-search">
</td>
</form>
<form name="form1" action="contracts.php" method="post" enctype="multipart/form-data">
<td style="position:relative;left:35px;">
<select name="Rows">
<option value="">Show Per Page</option>
<option value="1">- 1</option>
<option value="2">- 2</option>
<option value="3">- 3</option>
<option value="All">- All</option>
</select>
<input type="submit" name="btnrows" value="Submit" class="btn-search">
</td>
</form>
</tr>
</table>
<table style="table-layout:fixed">
<tr>
<td width="25" bgcolor="#A0B050">
<div class="tableTitle"></div></td>
<td width="25" bgcolor="#A0B050">
<div class="tableTitle"></div></td>
<td width="25" bgcolor="#A0B050">
<div class="tableTitle"></div></td>
<td width="70" bgcolor="#A0B050">
<div class="tableTitle">Contract Number</div></td>
<td width="70" bgcolor="#A0B050">
<div class="tableTitle">Contract Status</div></td>
<td width="55" bgcolor="#A0B050">
<div class="tableTitle">Annual Value of Contract</div></td>
<td width="80" bgcolor="#A0B050">
<div class="tableTitle">Original <br/>Date</div></td>
<td width="80" bgcolor="#A0B050">
<div class="tableTitle">Current <br/>Date</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Contract <br/>Length (Months)</div></td>
<td width="80" bgcolor="#A0B050">
<div class="tableTitle">Renewal <br/>Date</div></td>
<td width="110" bgcolor="#A0B050">
<div class="tableTitle">Site Name</div></td>
<td width="80" bgcolor="#A0B050">
<div class="tableTitle">Street</div></td>
<td width="80" bgcolor="#A0B050">
<div class="tableTitle">City</div></td>
<td width="100" bgcolor="#A0B050">
<div class="tableTitle">State or<br/>Province</div></td>
<td width="100" bgcolor="#A0B050">
<div class="tableTitle">Country</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Region</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Group <br/>Member</div></td>
<td width="100" bgcolor="#A0B050">
<div class="tableTitle">Group Name</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Rooms</div></td>
<td width="90" bgcolor="#A0B050">
<div class="tableTitle">Type of<br/>Establishment</div></td>
<td width="30" bgcolor="#A0B050">
<div class="tableTitle">CSF</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Food<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Pool<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Aqua<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Fire<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Crisis<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Eco<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Safety<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Room<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Quality<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Supply<br/>Check</div></td>
<td width="45" bgcolor="#A0B050">
<div class="tableTitle">Tour<br/>Check</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Number<br/>of Modules</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Currency<br/>of Invoice</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Number<br/>of Audits (Per Annum)</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Number<br/>of Visits (Per Annum)</div></td>
<td width="60" bgcolor="#A0B050">
<div class="tableTitle">Seasonal<br/>or Full Year</div></td>
<td width="65" bgcolor="#A0B050">
<div class="tableTitle">Month<br/>Open</div></td>
<td width="65" bgcolor="#A0B050">
<div class="tableTitle">Month<br/>Closed</div></td>
<td width="65" bgcolor="#A0B050">
<div class="tableTitle">Invoicing<br/>Profile</div></td>
</tr>

<?php
// find out how many rows are in the table
//  $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Quality_Safety='1'))
//select * from LIMIT WHERE {limit clause} AND (Quality_Safety='1') Order by Contract_Number ASC";

switch($search)
{
    case "AllContractsOld-New"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
  		$query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY ID ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by ID ASC";
        break;
		
    case "AllContractsNew-Old"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
  		$query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY ID DESC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by ID DESC";
        break;
		
    case "AllContractsOnHold"://complete but not tested with contracts
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Contract_Status='On Hold')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Contract_Status='On Hold'))
select * from LIMIT WHERE {limit clause} AND (Contract_Status='On Hold') Order by Contract_Number";
        break;
		
    case "AllContractsActive"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Contract_Status='Active')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Contract_Status='Active'))
select * from LIMIT WHERE {limit clause} AND (Contract_Status='Active') Order by Contract_Number";
        break;
		
    case "AllContractsTerminated"://complete but not tested with pageing
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Contract_Status='Terminated')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Contract_Status='Terminated'))
select * from LIMIT WHERE {limit clause} AND (Contract_Status='Terminated') Order by Contract_Number";
        break;
		
    case "ByCountryA-Z"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Country ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by Country ASC";
        break;
		
    case "ByCountryZ-A"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Country DESC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by Country DESC";
        break;
		
    case "ByRegionA-Z"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Region ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by Region ASC";
        break;
		
    case "ByRegionZ-A"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Region DESC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by Region DESC";
        break;
		
    case "ByNumberModulesO-X"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY No_of_Modules ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by No_of_Modules ASC";
        break;
		
    case "ByNumberModulesX-O"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY No_of_Modules DESC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by No_of_Modules DESC";
        break;
		
    case "ByContractValueLow-High"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
	$query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Annual_Contract_Value ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by Annual_Contract_Value ASC";
        break;
				
    case "ByContractValueHigh-Low"://complete
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Annual_Contract_Value DESC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} Order by Annual_Contract_Value DESC";
        break;
		
    case "ByFoodCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Food_Safety='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Food_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Food_Safety='1') Order by Contract_Number ASC";
        break;
		
    case "ByPoolCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Swimming_Pool_Safety='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Swimming_Pool_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Swimming_Pool_Safety='1') Order by Contract_Number ASC";
        break;
		
    case "ByAquaCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Legionella_Safety='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Legionella_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Legionella_Safety='1') Order by Contract_Number ASC";
        break;
		
    case "ByFireCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Fire_Safety='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Fire_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Fire_Safety='1') Order by Contract_Number ASC";
        break;
		
    case "ByCrisisCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Hurricane='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Hurricane='1'))
select * from LIMIT WHERE {limit clause} AND (Hurricane='1') Order by Contract_Number ASC";
        break;
		
    case "ByEcoCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Green='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Green='1'))
select * from LIMIT WHERE {limit clause} AND (Green='1') Order by Contract_Number ASC";
        break;
		
    case "BySafetyCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Guest='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Guest='1'))
select * from LIMIT WHERE {limit clause} AND (Guest='1') Order by Contract_Number ASC";
        break;
		
    case "ByRoomCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Room_Safety='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Room_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Room_Safety='1') Order by Contract_Number ASC";
        break;
		
    case "ByQualityCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Quality_Safety='1')";
	$query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Quality_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Quality_Safety='1') Order by Contract_Number ASC";
		break;
		
    case "BySupplyCheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Supply_Safety='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Supply_Safety='1'))
select * from LIMIT WHERE {limit clause} AND (Supply_Safety='1') Order by Contract_Number ASC";
        break;
		
    case "ByTourcheckYes"://complete but not by pageing as not enough in database
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Tourcheck='1')";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Contract_Number ASC) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%' AND (Tourcheck='1'))
select * from LIMIT WHERE {limit clause} AND (Tourcheck='1') Order by Contract_Number ASC";
        break;
		
	default:
		if ($searchKeyword == "") {
                $sql = "SELECT COUNT(*) FROM Intranet WHERE Contract_Number like '{$contractLike}%'";
        $query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber' FROM Intranet WHERE Contract_Number like '{$contractLike}%')
select * from LIMIT WHERE {limit clause} order by ID DESC";
        break;
		} else {
	$sql = "SELECT COUNT(*) FROM Intranet WHERE (Contract_Number like '%{$trimmed}%' or Site_Name like '%{$trimmed}%' or Street like '%{$trimmed}%' or City like '%{$trimmed}%' or State like '%{$trimmed}%' or Country like '%{$trimmed}%' or Region like '%{$trimmed}%' or Group_Name like '%{$trimmed}%' or Type_of_Establishment like '%{$trimmed}%' or Currency_of_Invoice like '%{$trimmed}%')";
	$query = "WITH LIMIT AS( SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber' FROM Intranet WHERE (Contract_Number like '%{$trimmed}%' or Site_Name like '%{$trimmed}%' or Street like '%{$trimmed}%' or City like '%{$trimmed}%' or State like '%{$trimmed}%' or Country like '%{$trimmed}%' or Region like '%{$trimmed}%' or Group_Name like '%{$trimmed}%' or Type_of_Establishment like '%{$trimmed}%' or Currency_of_Invoice like '%{$trimmed}%'))
select * from LIMIT WHERE {limit clause} order by ID DESC";
		break;
		}
}

$result = sqlsrv_query($conn, $sql);
$r = sqlsrv_fetch_array($result);
$numrows = $r[0];

// number of rows to show per page
 if ($rows == ""){
 $rowsperpage = 1;
 } else {
$rowsperpage = $rows;// find out total pages
}

 $totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage + 1;
$limit=($offset - 1) + $rowsperpage;
$query = str_replace('{limit clause}', "RowNumber BETWEEN $offset AND $limit", $query);

//echo $query;
$stmt = sqlsrv_query($conn, $query);

/******  build the pagination links ******/// range of num links to show
$range = 30;
echo "<div style=position:relative;height:30px;line-height:30px;><span style=font-family:Arial;font-size:14px;color:#333333;margin-top:9px;margin-bottom:5px;height:20px;margin-left:2px;>";
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
// was echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&Search=$search&q=$searchKeyword&Rows=$rows' style='color:#333333'><<</a>";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
// was echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&Search=$search&q=$searchKeyword&Rows=$rows' style='color:#333333'><</a> ";
} // end if

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
// was echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&Search=$search&q=$searchKeyword&Rows=$rows' style='color:#333333'>$x</a> ";
} // end else
} // end if
} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
// was echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&Search=$search&q=$searchKeyword&Rows=$rows' style='color:#333333'>></a> ";
// echo forward link for lastpage
// was echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&Search=$search&q=$searchKeyword&Rows=$rows' style='color:#333333'>>></a> ";
} // end if
/****** end build pagination links ******/
echo "</span>";
echo "<span style=font-size:13px;color:#555555;font-family:verdana;position:relative;width:250px;line-height:20px;top:-4px;left:30px;>Showing '$rowsperpage' per page.</span>";

if(isset($_GET['q']))
{
  $searchKeyword = @$_GET['q'];
  if ($searchKeyword=="") {
  } else {
  $trimmed = $searchKeyword;
  echo "<span style=position:relative;margin=0px;margin-bottom:5px;margin-left:20px;top:-3px;>Search results for your keyword: <strong>'" .$trimmed. "'</strong></span></div>";
  }
}

Its a lot i know,and I hope you get what my problem is.

Resolved thank you guys…

@multichild,

Good job. I see you are getting your head wrapped around php pretty well, based on your past few threads.

Hi cpradio, yes Im getting there and in honesty its your help and me learning from it that has pushed me up a level or two that I thought would take me much longer to get too.

I was at home after work, and was thinking it through and then all of a sudden it appeared and it was how you approached it. Came back into work next day applied it and hey presto it worked.

You have been a massive help, thank you.

I know its going ot be cheeky, but one more thing I’m trying to do in another thread is export that large contract tabel on view to a .csv file.

I dont know where to start.

For what it’s worth, i still advise against using a submit button for a check, due to IE’s behavior.