php+sqlserver分页最简单方法

听朋友说php+sqlserver分页很麻烦,今天研究了一下,发现其实比php+mysql还简单(实际上mysql也可以这样)。
下面这个函数就是取出符合纪录的第$page页的纪录到数组:
function selectPage($QueryStr,$page,$pageSize)
{
$R=@mssql_query($QueryStr);
if ($R){
$totalPage = ceil(mssql_num_rows($R)/$pageSize);
if (!$page ¦ ¦$page<=0) $page=1;
if ($page>$totalPage) $page=$totalPage;
$begin = ( $page-1 ) * $pageSize;
$ReArr=array();
$index=0;
if($pageSize>0) mssql_data_seek($R,$begin);
while($ResultArr=@mssql_fetch_array($R)){
if($pageSize>0)
if($index>$pageSize-1) break;
$ReArr[$index]=$ResultArr;
$index++;
}
return $ReArr;
}else
return false;
}

我觉得这样只用查询一次,用limit还有查询两次。实际上asp就是用这种方式!
欢迎各位提出意见
---------------------------------------------------------------

好,鼓励
---------------------------------------------------------------

r = $r; if (($p == "") ¦ ¦ (round($p) == 0)){ $p=1; } $this->m_currPage = $p; $this->m_RecordAmount = $RecordAmount; $this->m_PageAmount = Ceil($this->m_RecordAmount/$this->RecordRow_Per_aPage); $this->m_startIndex = ($this->m_currPage - 1) * $this->RecordRow_Per_aPage; $this->m_usingIndex = $this->m_startIndex; //calulate the current page if($this->m_currPage != $this->m_PageAmount) $this->m_nextPage = $this->m_currPage + 1; else $this->m_nextPage = $this->m_PageAmount; if($this->m_currPage != 1) $this->m_prevPage = $this->m_currPage - 1; else $this->m_prevPage = 1; } function Next_Record() { if(($this->m_usingIndex != $this->m_RecordAmount - 1) ¦ ¦ ($this->m_usingIndex < ($this->RecordRow_Per_aPage))) { $this->m_usingIndex ++; $result = 1; } else $result = 0; return $result; } function link_first() { echo " ``` 首页 ``` "; } function link_last() { echo " ``` 尾页 ``` "; } function link_prev() { echo " ``` 前页 ``` "; } function link_next() { echo " ``` 后页 ``` "; } function link_any() { echo " ```
直接到
``` "; } function getCurrent() { mssql_data_seek($this->r, $this->m_usingIndex); list($result) = mssql_fetch_array($this->r); return $result; } } ?> 凑个热闹^_* \--------------------------------------------------------------- 在MSSQL的mssql_query($string,$int); 其中$string可以是SQL SERVER存储过程的调用.这样就要自己写一个类似于limit的存储过程,在SQL SERVER他虽有TOP这项功能,但它的翻页,我们那时有个写VB的同事封装了一个用游标翻页的存储过程,由于年事以久,且不是自己封装的所一存储过程的代码找不见了,现在说一下调用过程,望能引发大家的一些思维:应该不是误导^_^ $string="exec web_ScrollPage 'fo03t001','complex,description,isroomreqd,isexcludegrp,isautoassign','complex,description,isroomreqd,isexcludegrp,isautoassign',".$PAGE_ROWS.",".$AddValue; exec web_ScrollPage 'fo03t001',---这一段是掉用存储过程,及所要访问的table 'complex,description,isroomreqd,isexcludegrp,isautoassign',----要读的字段 'complex,description,isroomreqd,isexcludegrp,isautoassign',----存储过程中建立临时表所要的字段 ".$PAGE_ROWS.",".$AddValue-----每页的行数,及开始的位置 在存储过程中是先建临时表,移游标到$AddValue所在的位置,然后移动游标一条条将记录读到临时表里,最后再读一次临时,删掉临时表,就这样是不是很烦. 不过在读大量数据是用数组翻页就不如这样了^_^
Published At
Categories with Web编程
comments powered by Disqus