2013-03-18 54 views
0

我想從數據庫中獲取所有結果並將它們返回到屏幕,問題是一旦我「回顯」它們全部顯示的值,但是當我使用「返回」時,只有1個出現。PHP OOP:如何獲取數據庫值並返回所有數據庫

我不能使用回聲,因爲我的應用程序是建立在它將html放入函數的方式上,所以我可以填充對象然後填充我的頁面內容。

這是我的代碼。

public function read() { 
    $query = "SELECT id, title, description, datetime FROM seminar"; 
    $result = $this->getDb()->query($query); 

    while(($row = $result->fetchObject()) !== false) { 
     foreach($row as $value) { 
     return $row->title; 
     //$this->setDescription($row->description); 
     //$this->setDatetime($row->datetime); 
     } 
    } 
} 

現在我想知道如何讓所有的值在屏幕上而不是它現在顯示的那個。

public function setSeminar($sem_obj) { 
    $this->sem_obj = $sem_obj; 
} 

public function render() { 
    $this->content = ' 
     On this page you can see all items. 
    '; 
    $this->content .= $this->sem_obj->getTitle(); 
    $this->content .= $this->sem_obj->getDescription(); 
    $this->content .= $this->sem_obj->getDatetime(); 
    //$this->content .= $this->text1->showSeminairs(); 
    return $this->content; 
} 

回答

0

使用數組..推databaseobjects並返回

$tempArray=array(); 
while(($row = $result->fetchObject()) !== false) { 

     $tempArray['title']=$row->title; 
    $tempArray['description']=$row->description; 
     return $tempArray; 

} 
+0

函數在不同的類中有什麼關係嗎? – 2013-03-18 08:37:24

+0

that shouldnot ..如果你正在調用thr類和函數corectly – bipen 2013-03-18 08:38:35

+0

我試過你的例子是這樣的:public function read(){ $ query =「SELECT id,title,description,datetime FROM seminar」; $ result = $ this-> getDb() - > query($ query); $ values = array(); ($ row = $ result-> fetchObject())!== false){foreach($ row as $ values){ $ values ['title'] = $ row-> title; 返回$值; // $ this-> setDescription($ row-> description); // $ this-> setDatetime($ row-> datetime); //} } }但我不知道如何填寫渲染功能 – 2013-03-18 08:43:56

0
public function read() { 
    $query = "SELECT id, title, description, datetime FROM seminar"; 
    $result = $this->getDb()->query($query); 
    return $result; 
} 

在調用方函數,把while循環並處理該值。

+0

功能在不同的類中有什麼關係嗎? – 2013-03-18 08:37:52

+0

這不是問題。無論您在哪裏打電話,它都會返回到該功能 – 2013-03-18 08:39:14

相關問題