Page 1 of 1

Class auto table

Posted: Sun Jun 24, 2007 10:17 pm
by webgroundz
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

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();
?>
i used it as my automatic form of table, depending on the result of the query

Posted: Sun Jun 24, 2007 10:30 pm
by Christopher
What is it used for?

Posted: Sun Jun 24, 2007 10:33 pm
by webgroundz
i used it to form an automatic table.

Posted: Sun Jun 24, 2007 10:37 pm
by feyd
Next step: abstraction away from interaction with SQL to a generic interface that could then be a result set from almost anything.

Posted: Sun Jun 24, 2007 10:38 pm
by Christopher
I don't think this is best solution. I would recommend separating acquiring the data into a separate object from the object presenting the data.

Posted: Sun Jun 24, 2007 10:40 pm
by webgroundz
arborint wrote:I don't think this is best solution. I would recommend separating acquiring the data into a separate object from the object presenting the data.
can you give me an example sir arborint.
thanks :D

Posted: Sun Jun 24, 2007 11:08 pm
by Christopher
webgroundz wrote:can u give me an example sir arborint.
thanks :D
Make an object that queries and fetches the rows. Pass that object to a second object that displays the table using the data from the data object.

Posted: Sun Jun 24, 2007 11:27 pm
by webgroundz
Make an object that queries and fetches the rows. Pass that object to a second object that displays the table using the data from the data object.
what is the use of separating the class into two classes?.
does it mean it will be more optimize?
thanks
:D

Posted: Sun Jun 24, 2007 11:29 pm
by feyd
webgroundz wrote:what is the use of separating the class into two classes?.
More modular.

Posted: Sun Jun 24, 2007 11:50 pm
by webgroundz
do i need to create an Interface?

Posted: Mon Jun 25, 2007 12:29 am
by Christopher
webgroundz wrote:do i need to create an Interface?
You don't necessarily need to use the "interface" definition, but you do want to define a clear interface to you classes -- that is essential.

Posted: Mon Jun 25, 2007 12:34 am
by webgroundz
arborint wrote:
webgroundz wrote:do i need to create an Interface?
You don't necessarily need to use the "interface" definition, but you do want to define a clear interface to you classes -- that is essential.
oh i see.. :)

thanks for the information.. i'll create an interface to fully organize my code..thanks

Posted: Mon Jun 25, 2007 1:06 am
by Christopher
It is important to note that in this system the display object has a dependency on the data object, but the data object has no dependency on the display object -- it is independent. Multiple display objects could use the data object and therefore it should have a clean interface.

In my opinion, this separation between the data and the presentation is one of he most important things that we programmers can do in our designs and implementations. Search for N-tier or multi-tier architecture for more information.