我的一个php_mysql分页类
2024-07-24 12:56:21
供稿:网友
class rspage {
var $debug = 0;
var $sqoe = 1; // sqoe= show query on error
var $link_id = 0;
var $record = array();
var $row;
var $numrow;
var $parse;
var $error = "";
var $m_timefmt = "yyyy-mm-dd hh24:mi:ss";
var $m_num=-1;
var $m_sql;
var $m_pagesize=10;
var $m_cmt;
var $m_curpage=1;
function open($db_link)
{
$this->link_id = $db_link;
}
function query($query_string,$page=1,$cnt=-1,$size=10) {
if($query_string=="")
{
echo "执行语句不能为空!";
return false;
}
if(!$page)
$page = 1;
if(!$cnt)
$cnt = -1;
$this->m_pagesize = $size;
if($this->parse){
mysql_free_result($this->parse);
}
if($cnt!=-1){
$this->m_num = $cnt;
}
else{
$result = mysql_query($query_string, $this->link_id);
if(!$result){
$this->error=mysql_error($this->link_id);
}
else{
$this->m_num = mysql_num_rows($result);
}
/*
$cntsql = "select count(*) max_num from ($query_string)";
$maxnum = 0;
$result = mysql_query($sqlstr,$this->link_id);
$ret = mysql_fetch_array($result);
mysql_free_result($result);
if(!$ret) {
$this->error=mysql_error($this->link_id);
}
else {
$this->m_num = $ret["max_num"];
}
*/
}
$pagecount = $this->getpagecount();
$pagesize = $this->getpagesize();
if($page>$pagecount)$page=$pagecount;
if($page<1)$page=1;
$this->m_curpage=$page;
$posbegin = ($page-1)*$pagesize;
$posend = $posbegin + $pagesize;
$query_string = "$query_string limit $posbegin,$posend";
$this->parse=mysql_query($query_string,$this->link_id);
if(!$this->parse) {
$this->error=mysql_error($this->link_id);
}
$this->row=0;
if($this->debug) {
printf("debug: query = %s<br>/n", $query_string);
}
if ($this->error && $this->sqoe)
echo "<br><font color=red><b>".$this->error["message"]."<br>query :/"$query_string/"</b></font>";
$numrow=mysql_num_rows($this->parse);
return $this->parse;
}
function settimeformat($timestr){
$this->m_timefmt = $timestr;
}
function next_record() {
if(!($row = mysql_fetch_array($this->parse))) {
mysql_free_result($this->parse);
$this->parse = false;
$stat=0;
}
else {
while(list($key,$val)=each($row)) {
$colreturn=strtolower($key);
$this->record[ "$colreturn" ] = $val;
if($this->debug) echo"<b>[$key]</b>:".$val."<br>/n";
}
$stat=1;
}
return $stat;
}
function record_exist() {
if(0 == mysql_num_rows($this->parse)) {
return 0;
} else {
return 1;
}
return $stat;
}
function seek($pos) {
$this->row=$pos;
}
function affected_rows() {
return mysql_affected_rows($this->parse);
}
function num_rows() {
return mysql_num_rows($this->parse);
}
function f($name) {
return $this->record[$name];
}
function p($name) {
print $this->record[$name];
}
function close() {
if($this->debug) {
//printf("disconnecting...<br>/n");
}
if($this->parse){
mysql_free_result($this->parse);
}
//mysql_close($this->link_id);
}
//---public 得到当前页
function getcurpage(){return $this->m_curpage;}
//---public 设定页长
function setpagesize($page_size){$this->m_pagesize = $page_size;}
//---public 得到页长
function getpagesize(){return $this->m_pagesize;}
//---public 得到纪录总数
function getcount(){return $this->m_num;}
//---public 得到页总数
function getpagecount(){
$page=floor($this->getcount()/$this->getpagesize());
if($this->getcount()%$this->getpagesize()>0)$page++;
return $page;
}
// 显示导航条。
// 参数说明: $url 调用页面ex:http://hello.php?showtype=main
function show_navibar($recname="条记录",$first="首页",$pre="上页",$next="下页",$last="末页"){
$showurl = getenv("request_uri");
$tmppos = strpos($showurl,"&rscnt=");
if(!$tmppos)
$tmppos = strpos($showurl,"?rscnt=");
if($tmppos)
$showurl = substr($showurl,0,$tmppos);
$url = $showurl;
$page = $this->getcurpage();
$pagecnt = $this->getpagecount();
$cnt = $this->getcount();
if($cnt < 0) // 没有分页,不显示任何东西。
return;
if(!strrchr($url,"?"))
$url = $url."?";
else // url已经带了参数。
$url = $url."&";
$url = $url."rscnt=".$cnt."&page=";
if($page>1){
?>
<a href=<?echo $url?>1><?=$first?></a> <a href=<?echo $url.($page-1)?>><?=$pre?></a>
<?
}
else{
?>
<?=$first?> <?=$pre?>
<?
}
if($page<$pagecnt){
?>
<a href=<?echo $url.($page+1)?>><?=$next?></a> <a href=<?echo $url.$pagecnt?>><?=$last?></a>
<?
}
else{
?>
<?=$next?> <?=$last?>
<?
}
?>
共有 <font color="#ff0000"><?=$cnt?></font> <?=$recname?>,当前<?=$page?>/<?=$pagecnt?>
第<input type=text style="width: 20px; height: 16px; font-size: 12px; color: #000000;border: 1px solid;" name=page value=<?=$page?>>页
<input type=button onclick="ice_ttttt_runto('<?=$url?>')" style="height: 16px; font-size: 12px; color: #000000;border: 1px solid;" value="go!">
<script language=javascript>
<!--
function ice_ttttt_runto(url){
var p = self.page.value;
self.navigate(url+p);
}
--></script>
<?
}
}