Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.
Popular code excerpts may be moved to "Code Snippets" by the moderators.
Moderator: General Moderators
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » 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
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
Last edited by
webgroundz on Sun Jun 24, 2007 10:37 pm, edited 3 times in total.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Jun 24, 2007 10:30 pm
What is it used for?
(#10850)
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Sun Jun 24, 2007 10:33 pm
i used it to form an automatic table.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jun 24, 2007 10:37 pm
Next step: abstraction away from interaction with SQL to a generic interface that could then be a result set from almost anything.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Jun 24, 2007 10:38 pm
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.
(#10850)
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Sun Jun 24, 2007 10:40 pm
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
Last edited by
webgroundz on Mon Jun 25, 2007 12:37 am, edited 1 time in total.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Jun 24, 2007 11:08 pm
webgroundz wrote: can u give me an example sir arborint.
thanks
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.
(#10850)
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Sun Jun 24, 2007 11:27 pm
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
Last edited by
webgroundz on Sun Jun 24, 2007 11:34 pm, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jun 24, 2007 11:29 pm
webgroundz wrote: what is the use of separating the class into two classes?.
More modular.
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Sun Jun 24, 2007 11:50 pm
do i need to create an Interface?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Jun 25, 2007 12:29 am
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.
(#10850)
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Mon Jun 25, 2007 12:34 am
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
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Jun 25, 2007 1:06 am
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.
(#10850)