从结果集中检索数据

时间:2013-06-19 10:19:20

标签: zend-framework html-lists

我正在开发一个应用程序,我需要从表中选择个人发表的评论..但是在从结果集中检索数据时遇到问题。 这是我的sql语句

public function get_Comments()
    {
        $select=$this->_db->select()
                      ->from('comments',array('id','comment','user_id'));
        $result=$this->getAdapter()->fetchAll($select);
        return $result;
    }

我已收到控制器中的数据并采取这种方式行动..           $ comments = new Application_Model_DbTable_Procedure();            $ main_comments = $评论 - > get_Comments();

       $result=(array) $main_comments;
       print_r($result);

我还没有将数据带到视图中,只是想直接从控制器显示它,然后再移动它。 这是我得到的数组

       Array ( [0] => Array ( [id] => 1 [comment] => how is attachment in safaricom    [user_id] => 2 ) [1] => Array ( [id] => 2 [comment] => hello world [user_id] => 2 ) [2] => Array ( [id] => 3 [comment] => how is attahment in kra [user_id] => 2 ) [3] => Array ( [id] => 16 [comment] => how is attachment in kra? [user_id] => 3 ) [4] => Array ( [id] => 17 [comment] => hello world [user_id] => 2 ) [5] => Array ( [id] => 18 [comment] => this is me trying everythin out [user_id] => 2 ) [6] => Array ( [id] => 19 [comment] => am testing system [user_id] => 3 ) ) 

     I wanted to get the result in three columns: 
          id       comment      user_id

我尝试过使用foreach循环,但是无法获得结果....我是zend的新手......我试图检查这个网站中的解决方案,但无法解决我的问题。

2 个答案:

答案 0 :(得分:0)

你的函数get_comments()返回一个fetchAll(双数组)结果,可以显示为: - 在你的控制器中:

 $main_comments=$comments->get_Comments();
$this->view->$main_comments=$main_comments;/*make the varibles avelaible to the view*/

- 在适当的视图中:

<?php foreach ($this->main_comments as $item): ?>
    <?= $item['comment'] ?> 
    <?= $item['user....'] ?> <!-- Add your vars here and some HTML...  -->
<?php endforeach ?>

希望它有所帮助。

答案 1 :(得分:0)

@Timmz,

你犯了错误,是:

$this->view->main_comments

不是

$this->view->$main_comments