ควรเริ่มทำอะไรก่อน
- ผมคิดว่าก่อนอื่นต้องหัดใช้ class บ่อยๆเป็นอันดับแรก ไม่ต้องเขียนเองหรอกครับ เข้าไปในเว็บนี้ http://www.phpclasses.org/ โหลดมาดูเยอะๆครับว่าเค้าเขียนกันอย่างไร
ตัวอย่าง class ที่ผมเขียนไว้ครับ PHP Table class by DS_Ohm Table.class.php
<?php
/*
* Table.class.php
*
* Copyright 2010 Narong <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/**
* @author narong
*/
class Table
{
/**
* HTML Table Tag Attributes
* @var string
*/
private $attributes;
/**
* HTML Table Tag caption.
* @var string
*/
private $caption;
/**
* Highlight table rows.
* @var array
*/
private $highlight = array();
/**
* Table header array contain title and css.
* @var array
*/
private $header = array();
/**
* Table data array
* @var array
*/
private $data = array();
/**
* Constructor
* Initialization that the object may need before it is used.
*
* @param $attributes
* @param $header
* @param $data
* @param $highlight
* @param $caption
* @return No value is returned.
*/
public function __construct($attributes, $header, $data, $highlight, $caption)
{
self::setTable($attributes, $header, $data, $highlight, $caption);
}
/**
* Set table
*
* @param $attributes
* @param $header
* @param $data
* @param $highlight
* @param $caption
* @return No value is returned.
*/
public function setTable($attributes, $header, $data, $highlight, $caption)
{
$this->attributes = $attributes;
$this->header = $header;
$this->data = $data;
$this->highlight = $highlight;
$this->caption = $caption;
}
/**
* Create table
*
* @return No value is returned.
*/
public function getTable()
{
$html = '<table ' . $this->attributes . '>';
$html .= '<caption>' . $this->caption . '</caption>';
$html .= '<thead>' . self::setHeader() . '</thead>';
$html .= '<tbody>' . self::setData() . '</tbody>';
$html .= '</table>';
return $html;
}
/**
* Set table header
*
* @return HTML Table header.
*/
private function setHeader()
{
$html = '<tr>';
if (!empty($this->header))
foreach ($this->header['title'] as $key=>$value)
$html .= '<th ' . $this->header['css'][$key] . '>' . $value . '</th>';
else
$html .= '<th class="txt-err">Please set header variable</th>';
$html .= '</tr>';
return $html;
}
/**
* Set table data
*
* @return HTML Table data
*/
private function setData()
{
$html = '';
if ($this->data) {
foreach ($this->data as $key=>$rows) {
$highlight = ($key%2 == 1) ? $this->highlight[0] : $this->highlight[1];
$html .= '<tr ' . $highlight . '>';
foreach ($rows as $value)
$html .= '<td>' . $value . '</td>';
$html .= '</tr>';
}
} else {
$html .= '<span class="txt-err">No data</span>';
}
return $html;
}
}
<?php
/*
* index.php
*
* Copyright 2010 narong <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
require_once dirname(__FILE__) . '/classes/Table.class.php';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>บัญชีรายชื่อสายทางที่น้ำท่วมขัง</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="generator" content="Geany 0.19.1" />
<link href="css/default.css" rel="stylesheet" type="text/css" />
</head>
<body>';
$attributes = 'id="mytable" cellspacing="0"';
$header['title'] = array( 'ID.', 'NAME', 'SURNAME', 'EMAIL');
$header['css'] = array(
'class="header-id"', 'class="header-name"',
'class="header-surname"', 'class="header-email"'
);
$highlight = array('class="odd"', 'class="even"');
$caption = 'Test table';
$data = array(
array('1', 'Narong', 'Rammanee', '[email protected]'),
array('2', 'Narong', 'Rammanee', '[email protected]'),
array('3', 'Narong', 'Rammanee', '[email protected]'),
array('4', 'Narong', 'Rammanee', '[email protected]'),
array('5', 'Narong', 'Rammanee', '[email protected]')
);
$table = new Table($attributes, $header, $data, $highlight, $caption);
echo $table->getTable();
echo '</body>
</html>';