听朋友说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就是用这种方式!
欢迎各位提出意见
---------------------------------------------------------------
好,鼓励
---------------------------------------------------------------
<?
//-----------------------
// 通用分页控制……
// 21bird 2002.8.22
//-----------------------
//class pagecontrol
class pagecontrol{
//properties
var $m_RecordAmount;
var $m_PageAmount;
var $m_currPage = 1;
var $RecordRow_Per_aPage = 10;
var $m_nextPage;
var $m_prevPage;
var $m_usingIndex;
var $m_startIndex;
var $r;
//methods
function pagecontrol($r, $RecordAmount, $p)
{
$this->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 "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=1\">首页</a>";
}
function link_last()
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $this->m_PageAmount . "\">尾页</a>";
}
function link_prev()
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" .$this->m_prevPage . "\">前页</a>";
}
function link_next()
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" .$this->m_nextPage . "\">后页</a>";
}
function link_any()
{
echo "<form method=get action=\"" . $_SERVER['PHP_SELF'] . "\">直接到<input type=text name=p size = 3>页<input type=submit value = 'Go!!!'></form>";
}
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所在的位置,然后移动游标一条条将记录读到临时表里,最后再读一次临时,删掉临时表,就这样是不是很烦.
不过在读大量数据是用数组翻页就不如这样了^_^
下面这个函数就是取出符合纪录的第$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就是用这种方式!
欢迎各位提出意见
---------------------------------------------------------------
好,鼓励
---------------------------------------------------------------
<?
//-----------------------
// 通用分页控制……
// 21bird 2002.8.22
//-----------------------
//class pagecontrol
class pagecontrol{
//properties
var $m_RecordAmount;
var $m_PageAmount;
var $m_currPage = 1;
var $RecordRow_Per_aPage = 10;
var $m_nextPage;
var $m_prevPage;
var $m_usingIndex;
var $m_startIndex;
var $r;
//methods
function pagecontrol($r, $RecordAmount, $p)
{
$this->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 "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=1\">首页</a>";
}
function link_last()
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $this->m_PageAmount . "\">尾页</a>";
}
function link_prev()
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" .$this->m_prevPage . "\">前页</a>";
}
function link_next()
{
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" .$this->m_nextPage . "\">后页</a>";
}
function link_any()
{
echo "<form method=get action=\"" . $_SERVER['PHP_SELF'] . "\">直接到<input type=text name=p size = 3>页<input type=submit value = 'Go!!!'></form>";
}
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所在的位置,然后移动游标一条条将记录读到临时表里,最后再读一次临时,删掉临时表,就这样是不是很烦.
不过在读大量数据是用数组翻页就不如这样了^_^
//
//divide the page
//
//分页显示
//
/////////////////////////////////////////////////////////////////////
//
//Parameter:
//
// $strLine -- the string line used to transfer Parameters
//
//Return value:
//
// $divide
//
/////////////////////////////////////////////////////////////////////function dividePage($strLine)
{
global $page,$maxpage;
$divide = "<CENTER>共";
$divide .= $maxpage;
$divide .= "页 当前第";
$divide .= $page;
$divide .= "页 ";
if($page != 1 and $maxpage != 1)
{
$p=$page-1;
$divide .= "<a href=\"".$PHP_SELF."?$strLine&page=$p\">上一页</a>";
$divide .= " ";
} if($page!=$maxpage and $maxpage != 1)
{
$p=$page+1;
$divide .= "<a href=\"".$PHP_SELF."?$strLine&page=$p\">下一页</a>";
$divide .= " ";
} $divide .= " 跳转到";
$divide .= "第<select name=select onchange=window.location.replace(this.value)>";
for($i=1;$i<=$maxpage;$i++)
{
if($i==$page)
{
$divide .= "<option value=index.php?$strLine&page=$i selected>".$i."</option>";
}
else
{
$divide .= "<option value=index.php?$strLine&page=$i>".$i."</option>";
}
}
$divide .= "</select>页</CENTER>"; return $divide;
}
?>