Taimaz_222
چهارشنبه 08 آذر 1391, 21:30 عصر
با سلام
من یه کلاس Table دارم که خروجیش اینه که رکوردهای جدول رو با توجه به Query و درخواستمون نمایش می ده .
کلاس Table:
<?
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'>مجموعا تعداد " . mysql_num_rows($Result) . " رکورد</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";
}
}
}
?>
و بدین صورت هم در برنامه ازش Object می سازمو استفاده می کنم :
.
.
.
$Sql = "SELECT username,name,family,email FROM Users";
$Result = new SqlTable($Sql);
$Result->HeaderStyle("font-size:14px;background-color: #3A87AD; color: #ffffff;");
$Result->TableStyle("background-color: #AFCD4B; color: #ffffff;width:550px;height:150px;");
$Result->RowStyle("font-size:15px;background-color:#BBD9EE;color:#000;");
$Result->FooterStyle("Background-color:#3A87AD;");
$Result->SetHost("localhost");
$Result->SetUser("root");
$Result->SetPass("");
$Result->SetDB("sana");
$Result->PrintTable();
و تصویر خروجی از کار این کلاس بدین صورت است :
95742
حال سوال من این است که :
چون اطلاعات من در بان فارسی است آیا می توان اطلاعات جدول را از راست به چپ نمایش داد ؟
درواقع فیلدها و اطلاعات محتوایشان از راست به چپ چیده شوند؟
باتشکر
من یه کلاس Table دارم که خروجیش اینه که رکوردهای جدول رو با توجه به Query و درخواستمون نمایش می ده .
کلاس Table:
<?
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'>مجموعا تعداد " . mysql_num_rows($Result) . " رکورد</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";
}
}
}
?>
و بدین صورت هم در برنامه ازش Object می سازمو استفاده می کنم :
.
.
.
$Sql = "SELECT username,name,family,email FROM Users";
$Result = new SqlTable($Sql);
$Result->HeaderStyle("font-size:14px;background-color: #3A87AD; color: #ffffff;");
$Result->TableStyle("background-color: #AFCD4B; color: #ffffff;width:550px;height:150px;");
$Result->RowStyle("font-size:15px;background-color:#BBD9EE;color:#000;");
$Result->FooterStyle("Background-color:#3A87AD;");
$Result->SetHost("localhost");
$Result->SetUser("root");
$Result->SetPass("");
$Result->SetDB("sana");
$Result->PrintTable();
و تصویر خروجی از کار این کلاس بدین صورت است :
95742
حال سوال من این است که :
چون اطلاعات من در بان فارسی است آیا می توان اطلاعات جدول را از راست به چپ نمایش داد ؟
درواقع فیلدها و اطلاعات محتوایشان از راست به چپ چیده شوند؟
باتشکر