Class auto table
Posted: Sun Jun 24, 2007 10:17 pm
Here's my sample class auto_table
instead of creating table manually, it provides automated table creation
html table based on the records in the table
please have a comment.
all your comments are pretty much appreciated. thanks
i used it as my automatic form of table, depending on the result of the query
instead of creating table manually, it provides automated table creation
html table based on the records in the table
please have a comment.
all your comments are pretty much appreciated. thanks
Code: Select all
<?php
class Auto_table
{
public $result;
private $count_field;
private $field_name;
private $ctr;
/**
* Fetch the result of query
*
* @param string $result
*/
public function __construct($result)
{
$this->result = mysql_query($result);
$this->count_field = $this->countField();
}
/**
*Count number of fields
*
* @return total number of fields
*/
private function countField()
{
return mysql_num_fields($this->result);
}
/**
* Get field name
*
* @param int $ctr
* @return field name
*/
private function getFieldName($ctr)
{
return mysql_field_name($this->result,$ctr);
}
/**
* This indicate the title of the column of a particular table
*
* @return string
*/
private function getTableTitle()
{
for($this->ctr=0; $this->ctr<=$this->count_field - 1; $this->ctr++){
$this->title = mysql_field_name($this->result,$this->ctr);
$str .= '<td align = center bgcolor=#F4F4F4>';
$str .= $this->title;
$str .= '</td>';
}
$str .= "</tr>";
return $str;
}
/**
* This indicate the data of a particular table
*
* @return string
*/
private function getTableData()
{
while($row = mysql_fetch_object($this->result))
{
$counter = 0;
$str .= "<tr>";
while($counter <= $this->count_field -1)
{
$field_name = $this->getFieldName($counter);
$str .= "<td bgcolor=white align = center>";
$str .= $row->$field_name;
$str .= "</td>";
$counter++;
}
//$str.= $this->showControls($view,$row->id);
$str.="</tr>";
}
return $str;
}
/**
* Display the table depends on the query
*
* @return auto table
*/
public function display()
{
$str .= '<table align=center border=0 bgcolor=gray cellpadding=3 cellspacing=1>';
$str .= $this->getTableTitle();
$str .= $this->getTableData();
$str .= '</table>';
return $str;
}
}
?>
<?php
/**
* sample usage
*/
include 'connect.php';
$sql = "SELECT * FROM tbl_tabinfo";
$auto = new Auto_table($sql);
echo $auto->display();
?>