PDA

View Full Version : خواندن اطلاعات بانک از راست به چپ



Taimaz_222
چهارشنبه 08 آذر 1391, 20: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

حال سوال من این است که :
چون اطلاعات من در بان فارسی است آیا می توان اطلاعات جدول را از راست به چپ نمایش داد ؟
درواقع فیلدها و اطلاعات محتوایشان از راست به چپ چیده شوند؟

باتشکر

$ M 3 H R D A D $
چهارشنبه 08 آذر 1391, 20:36 عصر
خوب از استایل dir:rtl استفاده کن دیگه هوم؟

Taimaz_222
چهارشنبه 08 آذر 1391, 20:41 عصر
دوست عزیز منظورم خواندن اطلاعات ازجدول دیتاست.
rtl که شما می گی اینجا کاربردد نداره :لبخندساده:

Unique
چهارشنبه 08 آذر 1391, 21:12 عصر
شما زمانی که query را تعین میکنی میتونی ترتیب فیلد ها را مشخص کنی ، از طریق $this->TableStyle هم میتونی با direction:rtl جدول را از راست به چپ ستون هاش را مرتب کنی ! پس کار خاصی نداره.

Taimaz_222
چهارشنبه 08 آذر 1391, 21:24 عصر
ممنون دوست عزیز . در Query اینکارو کردم و جواب میده اما در $this->TableStyle این تغییرات اعمال نمیشن چرا ؟
و اینکه چطور میشه اسامی ستون های جدول در بالا رو هم بتونم فارسی بنویسم ؟

username , name , family , email رو که خودکار از جدول واکشی می کنه و نمایش می ده رو بتونم فارسی تعریف کنم .
تشکر

Unique
جمعه 10 آذر 1391, 13:18 عصر
اما در $this->TableStyle این تغییرات اعمال نمیشن چرا ؟
بعید میدونم مشکلی باشه چون توی خط 75 داره style را به جدول اضافه میکنه. بعد از اینکه صفحه لود شد چک کنید کد html را ببینیند مشکل از کجاست ! شاید شما دارین دوباره ''=style میگذارین ! فقط direction:rtl کافی خواهد بود ! البته میتونید یک property مثلا class یا id بگذارین و بعد توی خود فایل css بدین و هر کاری میخواین با td ها و tr ها و خود table انجام بدین !


username , name , family , email رو که خودکار از جدول واکشی می کنه و نمایش می ده رو بتونم فارسی تعریف کنم .
توی خط 90 این کار انجام میبشه :

mysql_field_name($Result, $i)

کافیه یک آرایه با key هایی از نام فیلد ها و value های فارسی بسازین در واقع یک Property به نام $ColNames توی کلاس تعریف کنید که بتونین در زمان runtime بهش نام فیلد ها را بدین :

$Result->ColNames['name'] = "نام";
$Result->ColNames['family'] = "نام خانوادگی";
and ...



حالا تابع خط 90 را اینجوری بنویسین :

$this->ColNames[mysql_field_name($Result, $i)]