今日弄了点小东西,需要做一个sqlsrv的分页函数
最后也没弄成,算了,干脆有些东西写死了...
pdo_exequery为自己弄的一个pdo的查询封装函数
主要是基于这个处理:
preg_replace("/(t|T)(o|O)(p|P])\s\d{1,12}/","","select top 100 *");
首先要接收pageid及pagesize
if(isset($_GET["pageid"]))$intCurrentPage = $_GET["pageid"];
else
$intCurrentPage = 1;
$intPageSize = 20;
然后
设置一个分页时一个基准的orderid
$orderbyid="FItemID";
然后构建一个SQL用于获得总行数且包含分页的处理
$curcuror=$intCurrentPage*$intPageSize;
$SQL="select top $curcuror a.FNumber,a.FItemID,a.FName,a.FModel,d.fname as measureunits,e.fname as secmeasureunits from ".$tablename." a left join t_MeasureUnit d on a.FUnitID=d.FMeasureUnitID left join t_MeasureUnit e on a.FSecUnitID=e.FMeasureUnitID where 1=1 ";
然后进行替换,查询总行数
$newSQL=preg_replace("/(t|T)(o|O)(p|P])\s\d{1,12}/","",$SQL);
$ROW=pdo_exequery($pdo_msconnection,$newSQL);
$inumber=0;
if($ROW)$inumber=count($ROW);
最后是进行SQL的处理
if($intCurrentPage ==null)$intCurrentPage=1;
$intRecordTotal = $inumber;
if($intCurrentPage!="all"){
$intPageCount = intval(($intRecordTotal-1) / $intPageSize)+1;
if($intPageCount==0)$intPageCount=1;
if ($intCurrentPage > $intPageCount) {$intCurrentPage = $intPageCount;}
$intStart = $intPageSize * ($intCurrentPage - 1);
$offset=$intPageSize*($intCurrentPage-1);
if ($offset<0){$offset=0;}
//主要在于这里,进行了分页的SQL合并
$SQL="select * from (select top $intPageSize * from ($SQL)o1 order by $orderbyid desc)o2 order by $orderbyid asc";
}
else
{
$intCurrentPage=1;
$intPageCount=1;
}
$ROW=pdo_exequery($pdo_msconnection,$SQL);
$inumber=0;
if($ROW)$inumber=count($ROW);
此处即可进行数据的处理了
缺陷在于只能进行一个order by的处理,如果有其他需要,依样画葫芦,应该是没问题的