I wrote an earlier function to do something similar but in an effort to expand my PHP knowledge, I wrote this class. I'm just hoping some of you experts could give me some pointers on how to make it better. <? /****************************************************************/ /* SqlTable Class */ /* Written by Jared Testa */ /* www.jaredtesta.net */ /* Created 8/6/2006 */ /****************************************************************/ class SqlTable { //Declare class variables var $TableStyle; //Css Style Informasiton for Table var $HeaderStyle; //Css Style Information for Header var $RowStyle; //Css Style Information for each Row var $AltRowStyle; //Css Style Information for alternate Rows var $FooterStyle; //Css Style Information for Footer var $StrQuery; //Sql Statement var $QryType; //Type of Query var $Errors; //Array of any errors returned var $Host = "localhost"; //MySQL Host var $User = "user"; //MySQL User var $Pass = "pass"; //MySQL Password var $DB = "db"; //MySQL Database function SetHost($Value) //function to set MySQL Host { $this->Host = $Value; } function SetUser($Value) //function to set MySQL User { $this->User = $Value; } function SetPass($Value) //function to set MySQL Password { $this->Pass = $Value; } function SetDB($Value) //function to set MySQL DB { $this->DB = $Value; } function TableStyle($Style) //function to set CSS styles for table { $this->TableStyle = $Style; } function HeaderStyle($Style) //function to set CSS styles for header row { $this->HeaderStyle = $Style; } function RowStyle($Style) //function to set CSS styles for rows { $this->RowStyle = $Style; } function AltRowStyle($Style) //function to set CSS styles for alternate rows { $this->AltRowStyle = $Style; } function FooterStyle($Style) //function to set CSS styles for footer row { $this->FooterStyle = $Style; } function PrintTable() //function to print table or error list { if(empty($this->Errors)) { $link = mysql_connect($this->Host, $this->User, $this->Pass) or die('Could not connect: ' . mysql_error()); //build MySQL Link mysql_select_db($this->DB) or die('Could not select database'); //select database switch($this->QryType) { case "select": //SELECT STATEMENTS $Result = mysql_query($this->StrQuery); $Table = "<table style=\"{$this->TableStyle}\">"; if(mysql_error()) { $Table.="<tr style=\"{$this->HeaderStyle}\"><td>MySQL Error Occurred:</td></tr>"; $Table.="<tr style=\"{$this->RowStyle}\"><td>MySQL Error: " . mysql_error() . "</td></tr>"; } else { //Header Row with Field Names $NumFields = mysql_num_fields($Result); $Table.= "<tr style=\"{$this->HeaderStyle}\">"; for ($i=0; $i < $NumFields; $i++) { $Table.= "<td>" . mysql_field_name($Result, $i) . "</td>"; } $Table.= "</tr>"; //Loop thru results $RowCt = 0; //Row Counter while($Row = mysql_fetch_assoc($Result)) { //Alternate colors for rows if(isset($this->AltRowStyle)) { if($RowCt++ % 2 == 0) $Style = $this->RowStyle; else $Style = $this->AltRowStyle; } else { $Style = $this->RowStyle; } $Table.= "<tr style=\"$Style\">"; //Loop thru each field foreach($Row as $field => $value) { $Table.= "<td>$value</td>"; } $Table.= "</tr>"; } $Table.= "<tr style=\"{$this->FooterStyle}\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>"; } $Table.="</table>"; break; default: //Everything Else mysql_query($this->StrQuery); $Table = "<table style=\"{$this->TableStyle}\">"; if(mysql_error()) { $Table.="<tr style=\"{$this->HeaderStyle}\"><td>MySQL Error Occurred:</td></tr>"; $Table.="<tr style=\"{$this->RowStyle}\"><td>MySQL Error: " . mysql_error() . "</td></tr>"; } else { $Table.="<tr style=\"{$this->HeaderStyle}\"><td>Successfully Executed Query!</td></tr>"; $Table.="<tr style=\"{$this->RowStyle}\"><td>Query String: {$this->StrQuery}</td></tr>"; $Table.="<tr style=\"{$this->FooterStyle}\"><td>Query Affected " . mysql_affected_rows() . " Rows</td></tr>"; } $Table.="</table>"; break; } print($Table); } else { echo "The Following Errors occurred initializing the table class:<br />"; echo "<ul>"; foreach($this->Errors as $ErrDisc) { echo "<li>$ErrDisc</li>"; } echo "</ul>"; } } function SqlTable($Query) //Constructor function { $AppTypes = array("select", "delete", "insert", "update"); //Applicable Query Types $Query = trim($Query); //Trim excess spaces $Pos = strpos($Query, " "); //Find position of first space $Type = strtolower(substr($Query, 0, $Pos)); //Query type is the first word of the query if(in_array($Type, $AppTypes)) { //if the query type is in the array of applicable query types $this->StrQuery = $Query; $this->QryType = $Type; } else { //If not, produce error $this->Errors[] = "Query type ($Type) is not available"; } } } ?> PHP: You can Initialize it like this: <? require("sqltable.class.php"); $Sql = "SELECT * FROM TABLE"; $Result = new SqlTable($Sql); $Result->HeaderStyle("background-color: #006699; color: #ffffff;"); $Result->SetHost("localhost"); $Result->SetUser("user"); $Result->SetPass("password"); $Result->SetDB("database"); $Result->PrintTable(); ?> PHP:
Hi, Looks good. It is a good way to learn PHP. After you learn some stuff, you can try out some PHP frameworks. They make coding really easy. Code Ignitor is a free one that is really good. http://www.codeignitor.com Thomas