실행시
require_once("table.class");
$form->TableDisplay($rows);
# 실행 부분
# db_query_display.php
<?php
include("dbinfo.inc");
require_once("table.class");
$form = new Table;
$results = $db->query("SELECT * FROM user");
while($rows = $results->fetchRow()){
$form->TableDisplay($rows);
}
?>
# Class 정의
# table.class
<?php
class Table{
var $cnt;
function TableStyle(){
print<<<EOF
<style>
<!--
table {
border-collapse: collapse;
font-size: 12px;
}
table th, td{
border: 1px solid #CCCCCC;
line-height: 20px;
color: #666666;
padding-right: 10px;
padding-left: 10px;
}
-->
</style>n
EOF;
}
function TableHeader(){
$this->TableStyle();
echo "<table>n";
}
function TableBody($text){
echo "<tr>";
foreach($text as $key => $value){
if(!$this->cnt) echo "<th>".$key."</th>";
$td_display .= "<td>".$value."</td>";
}
if(!$this->cnt) echo "</tr><tr>n";
echo $td_display;
echo "</tr>n";
$this->cnt++;
}
function TableFooter(){
echo "</table>n";
}
function TableDisplay($value){
if(!$this->cnt) $this->TableHeader();
$this->TableBody($value);
//$this->TableFooter();
}
}
?>
------------------------------------------------------------------------------------------------
# DB 접속 정보 (pear로 했습니다만, 피어 안 써도 됩니다)
# dbinfo.inc
<?php
include_once("DB.php");
define("DB_TYPE", "mysql");
define("DB_HOST", "localhost");
define("DB_USER", "db_user");
define("DB_PASS", "db_password");
define("DB_NAME", "db_name");
$dns = DB_TYPE."://".DB_USER.":".DB_PASS."@".DB_HOST."/".DB_NAME;
$db = DB::connect($dns);
$db->setFetchMode(DB_FETCHMODE_ASSOC);
if(DB::isError($db)){
echo $db->getMassage();
exit;
}
?>
출처 : phpschool