1、分页类代码
<?php
/***
* php mysql 分页类
* 整理 http://www.
*/
class Pagination{
private $_result;
private $_count; //记录数
private $_pageMax; //最大页
private $_page; //当前页
private $_url;
private $_startPage;//分页条起码
private $_endPage; // 分页条止码
private $_nextPage; //上一页
private $_prePage; //下一页
function __construct($table, $pageSize, $getPage){
$this->_result = $GLOBALS['db']->query('SELECT * FROM '.$table);
$this->_count = $GLOBALS['db']->getRecordNum();
$this->_pageMax = ceil($this->_count/$pageSize);
$this->_result = '';
if($_GET[$getPage] == '')
$this->_page = 1;
else
$this->_page= max(intval($_GET[$getPage]), 1);
$this->_page = min($this->_page, $this->_pageMax);
$offset = ($this->_page - 1) * $pageSize;
$sql = 'SELECT * FROM '.$table.' LIMIT '. $offset .','. $pageSize;
$this->_result = $GLOBALS['db']->query($sql);
}
function getRecord(){
return $GLOBALS['db']->getRecord();
}
function getPageBar($url = '?', $barLn = 10, $style = 1){
if($style == 1){
if($barLn % 2 != 0 ){
$midder = ceil($barLn / 2);
$big_repair = $midder - 1 ;//当上面以进一法取整,则这里为减1,反之为加1
}else{
$midder = $big_repair = $barLn / 2;
}
$sml_repair = $midder- 1;
$this->_startPage = ($this->_page + $midder) > $this->_pageMax ? $this->_pageMax - $barLn : $this->_page - $sml_repair;
$this->_endPage = $this->_page < $midder ? $barLn : $this->_page + $big_repair;
}elseif($style == 2){
if($this->_page % $barLn == 0){
$this->_startPage = $this->_page;
}else{
$this->_startPage = ($this->_page > $barLn)? $this->_page - ($this->_page % $barLn ) : 1;
}
$this->_endPage = $this->_startPage + $barLn - 1;
}
$this->_url = $url;
$this->_nextPage = $this->_page + 1;
$this->_prePage = $this->_page - 1;
$this->_startPage = max($this->_startPage, 1);//至少从第一页开始
$this->_endPage = min($this->_endPage, $this->_pageMax);//最多只到末页
$this->_result = '当前是:<font color="#FF0000">'.$this->_page.'</font>/'.$this->_pageMax.'页,共<font color="#FF0000">'.$this->_count.'</font>条记录';
if ($this->_page > 1)
$this->_result .= '<a href="'.$this->_url.'page=1">
<font >9</font></a><a href="'.$this->_url. 'page='.$this->_prePage.'">
<font >3</font>
</a>';
else
$this->_result .= '<font >9</font>
<font >3</font>';
for($i = $this->_startPage; $i <= $this->_endPage; $i++) {
if ($this->_page == $i)
$this->_result .= '<font color="#ff0000">'.$i.'</font>';
else
$this->_result.= '<a href="'.$this->_url.'page='.$i.'">'.$i.'</a>';
}
if ($this->_page != $this->_pageMax) {
$this->_result .= '<a href="'. $this->_url .'page='.$this->_nextPage.'"><font >4</font></a>';
$this->_result .= '<a href="'.$this->_url.'page='.$this->_pageMax.'"><font >:</font></a>';
} else {
$this->_result.= '<font >4</font><font >:</font>';
}
$this->_result.= '<script type="text/javascript">
function chickForm(){
var submit = true;
var page_num=document.getElementById("page").value;
var exp=/^\d*$/;
var objExp=new RegExp(exp);
if(page_num==""){
alert("不能为空");
submit = false;
}else if(!objExp.test(page_num)){
alert("得是数字");
submit = false;
}
return submit;
}
</script>
<form method="get" action="'.$this->_url.'"onsubmit="return chickForm()">
<input type="text" id="page" name="page" />
<input type="submit" value="go" /></form>';
return $this->_result;
}
}
?>
2、调用示例
$page = new Pagination($table, 5, 'page');
while($row = $page->getRecord()){
echo $row[0],'<br/>';
}
echo $page->getPageBar('?',8, 1);
?>
开始本节的php 教程吧。
DIRECTORY_SEPARATOR (string) :目录分隔符
PATH_SEPARATOR (string) :路径分隔符
bool chdir ( string $directory )— 改变目录
例1,
echo getcwd() . "\n";
chdir('public_html');
echo getcwd() . "\n";
?>
bool chroot ( string $directory )— 改变根目录,仅在系统支持且运行于 CLI,CGI 或嵌入 SAPI 版本时才行。
dir::dir ( string $directory )— directory 类,有三个方法可用:read,rewind(将文件内部的位置指针重新指向一个数据流开头) 和 close
例2,
$d = dir("E:/work/html");
$methods = get_class_methods('dir');
print_r($methods);
while(false !== ($entry = $d->read())){
echo $entry."<br/>";
}
$d->close();
?>
void closedir ( resource $dir_handle )— 关闭目录句柄
例3,
$dir = "/etc/php5/";
if (is_dir($dir)){
if ($dh = opendir($dir)){
$directory = readdir($dh);
closedir($dh);
}
}
?>
string getcwd ( void )— 取得当前工作目录
resource opendir ( string $path [, resource $context ] )— 打开目录句柄
string readdir ( resource $dir_handle )— 从目录句柄中读取条目
例4,
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
closedir($handle);
}
?>
void rewinddir ( resource $dir_handle ) —将 dir_handle 指定的目录流重置到目录的开头
array scandir ( string $directory [, int $sorting_order [, resource $context ]] )— 列出指定路径中的文件和目录
例5,
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
print_r($files1);
print_r($files2);
?>
1、数据库操作类
//by http://www.
class mySql{
private $result;
private $conn;
public static $hasNew = false;
private __construct(){}
function __destruct(){
self::$hasnew=false;
}
function doNew(){
if(self::$have_new){
exit('数据库只能连接一次!');
}else{
self::$hasNew=true;
return new self;
}
}
private function connect($host,$user,$password,$dbname,$charset='utf8'){
$this->conn = mysql_connect()($host,$user,$password) or exit('错误码:'.mysql_errno(). '数据库连接失败:'.mysql_error());
mysql_select_db($dbname,$this->conn) or exit('错误码:'.mysql_errno().'选择数据库失败:'.mysql_error());
mysql_query()("set names $charset",$this->conn);
}
function query($sql,$buffer=true){
//mysql_real_escape_string($sql,$this->conn);//特殊字符义
if($buffer){
$this->result=mysql_query($sql,$this->conn) or exit('错误码:'.mysql_errno().'sql语句执行失败:'.mysql_error());
}else{
$this->result=mysql_unbuffered_query($sql,$this->conn) or exit('错误码:'.mysql_errno().'sql语句执行失败:'.mysql_error());
}
}
function getRecord(){
return mysql_fetch_array($this->result);
}
function close(){
mysql_free_result($this->result);
mysql_close($this->conn);
}
}
2、调用示例
//数据库
$db_host='localhost';
$db_user='root';
$db_pwd='root';
$db_name='news';
$charset='utf8';
$sql="select * from news_base";
$db=mySql::doNew();
$db->connect($db_host,$db_user,$db_pwd,$db_name,$charset='utf8');
$db->query($sql);
while($row=$db->getRecord()){
echo $row[1].'<br />';
}