[php]代码库
<?php
//分栏类
class ColumnClass {
var $page_size; //每页显示记录数
var $result; //分页记录集
var $page_cols; //栏目数
var $col_names = array () ; //各列显示名称数组
var $col_widths = array () ; //各列显示宽度数组
var $columns; //累计分栏显示标记
var $numbers; //显示当前页内序号
function __construct ( $page_size, $result, $page_cols,
$col_names, $col_widths) { //构造函数
$this->page_size = $page_size;
$this->result = $result;
$this->page_cols = $page_cols;
$this->col_names = $col_names;
$this->col_widths = $col_widths;
$page_res = $page_size / $page_cols; //每栏显示记录数
while ($row = mysql_fetch_array ($this->result)) { //将分页记录集存入二维数组
$records [] = $row;
}
$numbers = 0;
$this ->columns. = " <table align =center border =1
cellpadding =0 rules =none class ='tab' style ='border -collapse:
collapse'>" ; //建立页面表格
$this->columns. = " <tr>" ;
for ($i = 0; $i < $this->page_cols; $i++) { //循环建立栏目表格
$this->columns. = " <td>" ;
$this->columns. = " <table align=center border=1 class=
'tab' style='border-collapse:collapse'>" ;
$this->columns. = " <tr>" ;
$this->columns. = " <th width=40>序号</th>" ;
for ($j = 0; $j < (sizeof ($records [0]) / 2) ; $j++)
//根据各列显示宽度呈现各列显示名称
{
$this->columns. = " <th width=" .$this->col_widths
[$j] ." >" .$this->col_names [$j] ." </th>" ;
}
$this->columns. = " </tr>" ;
for ($k = $i * $page_res; $k < ($i + 1) * $page_res; $k++)
//循环显示数据记录
{
$this->columns. = " <tr>" ;
if (sizeof ($records [$k]) == 0) { //是否显示序号
$this->columns. = " <td> </td>" ;
} else {
$numbers++;
$this->columns. = " <td>$numbers</td>" ;
}
for ($j = 0; $j < (sizeof ($records [0]) / 2) ; $j++)
//逐个显示字段值
{
if (sizeof ($records [$k]) == 0) { //是否显示字段值
$this->columns. = " <td> </td>" ;
} else {
$this->columns. = " <td>" .$records [$k] [$j] ."
</td>" ;
}
}
$this->columns. = " </tr>" ;
}
$this->columns. = " </table>" ;
$this->columns. = " </td>" ;
}
$this->columns. = " </tr>" ;
$this->columns. = " </table>" ;
}
}
?>
//使用方法
<?php
//指定参数
$page_size=50; //每页显示记录数
$page_cols=2; //栏目数
$col_names=array (' 编号’,姓名',' 班级',’状态’) ;
//各列显示名称数组
$col_widths=array ('100',’80’,'160',’70’) ;
//各列显示宽度数组
//调用分页类
include (" pageclass.php") ; //包含类文件
$pageObj=new PageClass ($nums,$page_size,$page) ;
//声明分页对象,包含3 个参数:记录总数、每页显示记录数、
//当前页码
$sql=" select num,name,dept,status from ksb limit " .$pageObj->
offset." ," .$pageObj->page_size; //分页SQL
$result=mysql_query ($sql) ; //生成分页记录集
//调用分栏类
include (" columnclass.php") ; //包含类文件
$colObj=new ColumnClass ($page_size,$page_cols,$result,
$col_names,$col_widths) ; //声明分栏对象,包含5 个参数:每
//页显示记录数、分页记录集、栏目数、各列显示名称及显示
//宽度
echo $colObj->columns; //分栏显示
?>
[代码运行效果截图]