2013-03-18 102 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

相关问题