0

我试图从数据库中获取所有结果并将它们返回到屏幕,问题是一旦我“回显”值它们都显示出来,但是当我使用“返回”时只有 1 显示。

我不能使用 echo,因为我的应用程序是建立在将 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;
}
4

2 回答 2

0

使用数组..推送数据库对象并返回

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

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

} 
于 2013-03-18T08:29:34.313 回答
0
public function read() {
    $query = "SELECT id, title, description, datetime FROM seminar";
    $result = $this->getDb()->query($query);
    return $result;
}

在调用者函数中,放置while循环并处理值。

于 2013-03-18T08:32:21.460 回答