最近开发论坛LightBB时,写了些实用的类
Connection类,是PHP操作MySQL的简单包裹类
class Connection{ var $host; var $user; var $passwd; var $database; var $conn; function Connection($host, $user, $passwd, $database) { $this->host = $host; $this->user = $user; $this->passwd = $passwd; $this->database = $database;}
function Open() {$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or die("Could not connect to $this->host");
mysql_select_db($this->database,$this->conn) or die("Could not switch to database $this->database");
}
function Close() { mysql_close($this->conn);}
function GetConnID() {return $this->conn;
}
}
?>
Dataset类,对SQL查询结果进行封装,本来想做.NET那种离线操作,然后做个Adapter类,并计划自动cache技术,但暂时未实现
<?
class Dataset{ var $result;var $rows; //二维数组
var $fileds; var $rowsNum; var $fieldsNum; var $queryStr; function Dataset($queryStr) { $this->queryStr = $queryStr; $this->rows= array(); $this->fields = array(); $this->rowsNum = 0; $this->fieldsNum = 0;}
function Load() {$this->result = mysql_query($this->queryStr) or die("Could not query database");
$this->rowsNum = mysql_num_rows($this->result);
$this->fieldsNum = mysql_num_fields($this->result);
for($i=0;$i<$this->fieldsNum;$i++)
{ $this->fields[$i]=mysql_field_name($this->result,$i);//取第i个字段的名称
}
while($r = mysql_fetch_assoc($this->result))
{ array_push($this->rows, $r);}
}
function Update() {}
function CacheToFile() {}
function RetriveFromCache() {}
function GetRows() { return $this->rows;
}
function GetFields() {return $this->fields;
}
function GetRow($rno) {return $this->rows[$rno];
}
function Getfiled($fno) {return $this->fields[$fno];
}
function GetItem($rno, $fno) {return $this->rows[$rno][$fno];
}
function GetRowsNum() { return $this->rowsNum;
}
function GetFieldsNum() {return $this->fieldsNum;
}
function PrintTable() { print "<table>";foreach($this->rows as $row)
{ print "<tr>";foreach($row as $item)
{print "<td>".$item."</td>";
}
print "</tr>";}
print "</table>";}
function PrintFields() {foreach($this->fields as $fieldname)
{ print $fieldname." ";}
}
}
?>
<? //test code/*$conn = new Connection("localhost", "root", "", "forum");$conn->Open();$ds = new Dataset("select * from tbl_categories");$ds->Load();$ds->PrintTable();$rows = $ds->GetRows();print_r($rows);$conn->Close();*/?>
Pager类,分页功能
<?php
/*if(basename($HTTP_SERVER_VARS['PHP_SELF']) == "pager.class.php") { header("HTTP/1.0 404 Not Found");}*/?>
<? require_once("connection.class.php"); ?><? require_once("dataset.class.php"); ?><?php
class Pager{ var $sql;var $url='';
var $itemsPerPage=3;var $paraName = "p";
var $totalItemsNum=0; var $totalPagesNum=0; var $currentPageNo=1; var $ds; function Pager($sql,$itemsPerPage, $paraName) //examaple: "select * from tbl_topics", "viewtopics.php?board=1", 1, 3, "p"
{ $this->sql=$sql; $this->itemsPerPage=$itemsPerPage; $this->paraName=$paraName;$this->url=$this->GetSelfURL();
$this->currentPageNo=empty($_GET["$paraName"]) ? 1 : $_GET["$paraName"];
$this->ds = new Dataset($this->sql);
$this->ds->Load();$this->totalItemsNum=$this->ds->GetRowsNum();
$this->totalPagesNum=ceil($this->GetTotalItemsNum()/$this->itemsPerPage);
}
function GetTotalItemsNum() { return $this->totalItemsNum;
}
function GetTotalPagesNum() { return $this->totalPageNumber;
}
function GetItemsStart() { $start=($this->currentPageNo-1)*$this->itemsPerPage;
return $start; }
function GetItemsLimit() { return $this->itemsPerPage;
}
function GetItemsCurrentPage() //You can format the dataset(array)
{$arr = array();
while($i<$this->GetItemsLimit() && $i+$this->GetItemsStart() < $this->totalItemsNum)
{$arr[$i]=$this->ds->GetRow($this->GetItemsStart()+$i);
$i++;
}
return $arr;}
/* --utilty functions------------------------------------------------*/ function PrintPagerBar() { print "<span class='pagerbar'>";for($i=1; $i<=$this->totalPagesNum; $i++)
{$this->url.=(stristr($this->url,'?')!=false)?'':'?';
if(($start = strpos($this->url,$this->paraName."="))!=false)
{$newurl= substr_replace($this->url, $this->paraName."=".$i, $start);
}
else {$newurl = $this->url."&".$this->paraName."=".$i;
}
print "<a href='";print $newurl;
print "'>";print $this->currentPageNo==$i ? "<strong>".$i."</strong>" : $i;
print " </a>";}
print "</span>"; }
function PrintItemsCurrentPage() { print "<table>";foreach($this->GetItemsCurrentPage() as $row)
{ print "<tr>";foreach($row as $item)
{print "<td>".$item."</td>";
}
print "</tr>"; }
print "</table>";}
/*-Private-----------------------------------------------------------*/function GetSelfURL() //Get Current Page URL
{ //only for .PHP fileif(!empty($_server["REQUEST_URI"]))
{ $scriptName = $_SERVER["REQUEST_URI"]; $nowurl = $scriptName;
}else{ $scriptName = $_SERVER["PHP_SELF"];if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;
else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
}
return $nowurl; }
}
?>
<? /*----------------------------------------------------------------*/ ?><?
?>
<? //test code /* $conn = new Connection("localhost", "root", "", "forum"); $conn->Open(); $ipager = new Pager("select * from tbl_topics", 2, "page"); $ipager->PrintPagerBar(); $ipager->PrintItemsCurrentPage(); $conn->Close(); */ ?>
浙公网安备 33010602011771号