php实现一个输出HTML TABLE的类
时间:2016-07-03
本文章向大家分享一个PHP输出HTML table的类,如果在某个页面需要使用到table,我们可以直接调用这个类输出table,这样很方便,具体php代码请看一下。
php实现一个输出HTML TABLE的类,源代码如下:
<?php
class Table{
var $table_array = array();
var $headers = array();
var $cols;
function Table( $headers ){
$this->headers = $headers;
$this->cols = count ( $headers );
}
function addRow( $row ){
if ( count ($row) != $this->cols )
return false;
array_push($this->table_array, $row);
return true;
}
function addRowAssocArray( $row_assoc ) {
if ( count ($row_assoc) != $this->cols )
return false;
$row = array();
foreach ( $this->headers as $header ) {
if ( ! isset( $row_assoc[$header] ))
$row_assoc[$header] = " ";
$row[] = $row_assoc[$header];
}
array_push($this->table_array, $row) ;
}
function output() {
print "<pre>";
foreach ( $this->headers as $header )
print "<B>$header</B> ";
print "\n";
foreach ( $this->table_array as $y ) {
foreach ( $y as $xcell )
print "$xcell ";
print "\n";
}
print "</pre>";
}
}
class HTMLTable extends Table{
var $bgcolor;
var $cellpadding = "2";
function HTMLTable( $headers, $bg="#ffffff" ){
Table::Table($headers);
$this->bgcolor=$bg;
}
function setCellpadding( $padding ){
$this->cellpadding = $padding;
}
function output(){
print "<table cellpadding=\"$this->cellpadding\" border=1>";
foreach ( $this->headers as $header )
print "<td bgcolor=\"$this->bgcolor\"><b>$header</b></td>";
foreach ( $this->table_array as $row=>$cells ) {
print "<tr>";
foreach ( $cells as $cell )
print "<td bgcolor=\"$this->bgcolor\">$cell</td>";
print "</tr>";
}
print "</table>";
}
}
$test = new HTMLTable( array("a","b","c"), "#00FF00");
$test->setCellpadding( 7 );
$test->addRow( array(1,2,3));
$test->addRow( array(4,5,6));
$test->addRowAssocArray( array ( 'b'=>0, 'a'=>6, 'c'=>3 ));
$test->output();
?>
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC