|
 |
|
สอบถาม " การเรียกข้อมูลจาก SQL มาแสดง โดยใช้รูปแบบ OOP " พอดีมีปัญหานิดหน่อย |
|
 |
|
|
 |
 |
|
ครับ เนื่องจากต้องการแสดง ข้อมูล ใน Table นั้นๆ โดยใช้แบบ OOP
แต่ผมติดปัญหาเรื่องการ Loop ข้อมูลมาแสดง
$account_id = new Database();
$account_id->Connect();
$account_id->Query('login',array('userid'=>$_SESSION['userid']),null,1);
$char_id = new Database();
$char_id->Connect();
$char_id->Query('char',array('account_id'=>$account_id->Rows['account_id']),'char_num',null);
echo '<option value="'.$char_id->Rows['char_id'].'">'.$char_id->Rows['name'].'</option>';
$account_id->Disconnect();
$char_id->Disconnect();
ตอนนี้ พิมพ์ข้อมูล ออกมาแค่ 1 อัน
ผมลองศึกษา foreach ก็เข้าใจนิดๆหน่อยๆ แต่พอเอามาปรับประยุกต์ ก็ยังงงๆอยู่
ยังไงรบกวนชี้แนวทางด้วยครับ : )
Code (PHP)
public function Query($table,$where = null,$order = null,$limit = null)
{
$select = 'SELECT * FROM `'.$table.'` WHERE ';
if($where != null)
{
$keys_where = array_keys($where);
for( $i = 0 ; $i < count($where) ; $i++)
{
$select .= $keys_where[$i].'='.$where[$keys_where[$i]];
if($keys_where[$i+1] != null)
{
$select .= ' AND ';
}
}
}
if($order != null)
{
$select .= ' ORDER BY '.$order;
}
if($limit != null)
{
$select .= ' LIMIT '.$limit;
}
$query = @mysql_query($select) or die(mysql_error());
$this->NumRows = mysql_num_rows($query);
if($query)
{
$this->Rows = mysql_fetch_assoc($query);
return true;
} else {
return false;
}
}
public function Rows($field)
{
return $this->Rows[$field];
}
Tag : PHP, MySQL

|
ประวัติการแก้ไข 2012-08-18 16:19:49
|
 |
 |
 |
 |
Date :
2012-08-18 13:22:52 |
By :
blackedition |
View :
1150 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ผมลองทำแบบ
Code (PHP)
$account_id = new Database();
$account_id->Connect();
$account_id->Query('login',array('userid'=>$_SESSION['userid']),null,1);
$account_id->Disconnect();
$char_id = new Database();
$char_id->Connect();
$char_id->Query('char',array('account_id'=>$account_id->Rows['account_id']),'char_num',2);
$resultArray = array();
for ($i = 0;$i<$char_id->NumRows();$i++) {
$result = $char_id->Rows['char_id'];
array_push($resultArray,$result);
}
foreach($resultArray as $values)
{
echo '<option value="'.$values.'">'.$values.'</option>';
}
ก็แสดงผลตามจำนวนที่มีใน Table นั้นๆ เช่นมีข้อมูลแค่ 2 ก็ มี Select มาให้ 2 อันตรงตามข้อมูฃ
แต่ปรากฏว่า .. ค่าที่แสดงออกมา มันซ้ำกัน น่าจะผิดตรง $result = $char_id->Rows['char_id'];
ยังไงรบกวนด้วยครับ
|
 |
 |
 |
 |
Date :
2012-08-18 22:07:31 |
By :
blackedition |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
ขอศึกษาหากสงสัยเด๊วอาจต้องรบกวนอีกครั้งนะครับ
|
 |
 |
 |
 |
Date :
2012-08-24 21:39:32 |
By :
blackedition |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับผม
โดยแก้ไขดังนี้
Code (PHP)
if($query)
{
$this->Rows = mysql_fetch_assoc($query);
return true;
} else {
return false;
}
แก้เป็น
Code (PHP)
if($query)
{
$resultArray = array();
while($obResult = mysql_fetch_array($query))
{
array_push($this->Rows,$obResult);
}
return true;
} else {
return false;
}
และใช้ foreach การแสดงผล
Code (PHP)
foreach($db->Rows as $arr)
{
echo $arr['name']."<br>";
}
ก็จะแสดงข้อมูลทั้งหมดออกมา
ขอบคุณ Mr.Win มากครับ หลังจาก งงและลองแก้ไขเอง อยู่ 2 วัน
|
 |
 |
 |
 |
Date :
2012-08-24 21:53:31 |
By :
blackedition |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|