最新php

本文介绍了一种使用PHP实现的分页查询技术,通过自定义分页类来处理数据库查询结果的分页显示。该方法可以有效地管理和展示大量数据,同时提供用户友好的浏览体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php

/*--------------------------------------------------------------------

$ module: page.class.php

$ license: version 1.0

$ create: 2009-7-17 15:21:12

$ author: LovelyLife <master#onlyaa.com>

$ copyright :Copyright 2006 onlyaa.com

----------------------------------------------------------------------*/





// 分页模板

class CLASS_PAGE {

var $html;

var $url;

var $visiblesize;  // 显示长度

var $table;    // 用来区分多表分页访问

var $page;    // 地址中的查询字段

var $current;   // 当前访问页

var $totalsize;   // 记录总数

var $pagesize;   // 分页大小

var $pagecount;   // 页数

var $first;

function __construct($cfg) {

  $this->html = "";

  $this->url = "";

  $this->first = 1;

  $this->visiblesize = 10;

  $this->page = "page";

  // 默认分页大小为 10

  $this->pagesize = intval($cfg["pagesize"]);

  if($this->pagesize < 1) {

   $this->pagesize = 10;

  }

 

  // 记录数

  $this->totalsize = intval($cfg["totalsize"]);

  $this->pagecount = ceil($this->totalsize / $this->pagesize);

  $this->pagecount = max($this->pagecount, 1); // 修正页数为1的bug

  // 当前访问页

 

  if($cfg["html"] == true) {

  

   $cur = intval($cfg["page"]);

   $cur = max(1, $cur);

   $this->current = min($this->pagecount, $cur);

   $this->first = $cfg["first"];

   $this->url = $cfg["tpl"];

  

   // $this->_htmlurl($cfg["tpl"]);

  } else {

   if(!isset($_GET[$this->page])) {

    $this->current = 1;

   } else {

    $cur = intval($_GET[$this->page]);

    $cur = max(1, $cur);

    $this->current = min($this->pagecount, $cur);

   }

   $this->_baseURL();

  }

  //var_dump($this);

}



function CLASS_PAGE($cfg) { $this->__construct($cfg); }



function GetCurrentPage() {

  return $this->current;

}



function _first(){ return 1; }



function _prev() { return max(1, $this->current - 1); }



function _next() { return min($this->current + 1, $this->pagecount); }



function _last() { return $this->pagecount; }



function __toString($mode = 1) {

  switch($mode) {

   case 2:

    break;

   case 3:

    break;

   default:

    $this->defaultview();

  }

  return $this->html;

}



function defaultview() {

  $cur = $this->current;

  $pcount = $this->pagecount;

  if($cur > 1) {

   $this->html = " <a href=/"".$this->goto($this->_prev())."/"><font size=2>[<<]</font></a> ";

  }

 

  $buf = ""; // 分页缓冲区

  $start = $cur - (floor($this->visiblesize/2));

  $start = max(1, $start);

 

  $t = $cur + floor($this->visiblesize/2) - 1; // last visible page id

  $end = min($pcount, $t); // 显示分页长度

 

  for($i=$start; $i <= $end; $i++) {

   if($i == $cur) {

    $buf .= "".$i." ";

    continue;

   } else {

    $buf .= "<a href=/"".$this->goto($i)."/">[{$i}]</a> ";

   }

  }

 

  $this->html .= $buf;

  // print("<br>cur:{$cur}<br>");

  if($cur < $pcount) {

   $this->html .= "<a href=/"".$this->goto($this->_next())."/"><font size=2>[>>]</font></a>";

  }

 

  // $this->html = "<div style=/"font-family:arial,宋体; font-size: 13px; padding-left:10px;font-size:14px;word-spacing:4px; text-align: left;/">".$this->html."</div>";

}



function goto($pageid) {

  if($pageid > 1) {

   $url = str_ireplace("{id}", $pageid, $this->url);

  } else {

   $url = $this->first;

  }

 

  return $url;

}



function _baseURL() {

  $url = $_SERVER["REQUEST_URI"];

  $pos = strpos($url, "?");

  // 没有其他的参数

  if($pos == -1) {

   $this->url=$url."?".$this->page."=";

   return;

  }

 

  // 存在其他参数

  $params = split("&", $_SERVER["QUERY_STRING"]);

  $params2 = array();

  foreach($params as $value) {

   $l = split("=", $value);

   if($l[0] != $this->page) {

    array_push($params2, $value);

   }

  }

 

  $this->url = $_SERVER["SCRIPT_NAME"]."?".implode($params2, "&")."&page={id}";

  $this->first = str_ireplace("{id}", 1, $this->url);

}

}
?>


====================================

修改后的内容

<?php
error_reporting(E_ALL);
define('SCR','index');
require_once('global.php');
require_once('page.class.php');  // 包含page类
!$winduid && Showmsg('not_login');
require_once(R_P.'require/header.php');
mysql_select_db("bbs");
$exec="select nasipaddress,acctstarttime,acctstoptime,acctsessiontime,acctInputOctets,acctOutputOctets,acctterminatecause from radacct where username='$windid' order by acctstarttime desc";
$result=mysql_query($exec);
$num=mysql_num_rows($result);

// 这里先分页,再查询sql
   $totalsize = $num;      // 记录总数
   $pageSize = 20;   // 分页大小
   $cfg = array("totalsize" => $totalsize,"pagesize" => $pageSize);
   $cfg["page"] = "pageid";  // 设置分页标识 url中问号后面
   $pager = new CLASS_PAGE($cfg);  // 创建分页对象
   $sql = $exec." limit ".($pager->GetCurrentPage()-1)*$pageSize .",{$pageSize};"; // 组合分页查询sql

// 分完页后,在执行分页查询的sql
$result2=mysql_query($sql);
$rsNum=mysql_num_rows($result2);

if($rsNum <= 0) {
  echo"你还没有成功登录过VPN,哪儿来的详细记录?";
} else {

echo "<table  width=100%>";
echo "<tr><td>";
echo "<font size=4><b>服务器</b></font></td>";
echo "<td>";
echo "<font size=4><b>开始时间</b></font></td>";
echo "<td>";
echo "<font size=4><b>停止时间</b></font>";
echo "<td>";
echo "<font size=4><b>时长</b></font></td>";
echo "<td>";
echo "<font size=4><b>上传</b></font></td>";
echo "<td>";
echo "<font size=4><b>下载</b></font></td>";
echo "<td>";
echo "<font size=4><b>中断原因</b></font></td></tr>";

// 显示分页记录
while($rs=mysql_fetch_object($result2)) {
  $nasipaddress = $rs->nasipaddress == "192.168.0.70" ? "国内1号加速服务器" : $rs->nasipaddress ;
  echo "<tr height=25px><td>";
  echo "".$nasipaddress."";
  echo "<td>";
  echo "".$rs->acctstarttime."</td>";
  echo "<td>";
  echo "" .$rs->acctstoptime."</td>" ;
  echo "<td>";
  echo ""  .$rs->acctsessiontime."秒</td>" ;
  echo "<td>";
  echo ""  .$rs->acctInputOctets."字节</td>" ;
  echo "<td>";
  echo ""  .$rs->acctOutputOctets."字节</td>" ;
  echo "<td>";
  echo ""  .$rs->acctterminatecause."</td>" ;
  echo "</tr>";
  }
  echo "</table>";
  echo "<p>";
  echo "</p>";
  echo "用户:$windid";
  echo "<p>";
  echo "</p>";
  echo "<p>";
  echo "</p>";
  echo "共查询到".$num."条记录";
  echo "<hr />";
  ///delete
  // echo yemiandaohang("acct.php?",$num,$perpage,$page);
  echo $pager->__toString();  // 输出
}
footer();
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值