0

我正在开发一个应用程序,我需要从表中选择个人发表的评论..但是在从结果集中检索数据时遇到问题..这是我的 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=$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 的新手...我尝试在此站点中检查解决方案,但找不到任何解决我的问题的方法。

4

2 回答 2

0

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

 $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 ?>

希望能帮助到你。

于 2013-06-19T10:34:57.013 回答
0

@蒂姆兹,

你犯了一个错误,它是:

$this->view->main_comments

不是

$this->view->$main_comments
于 2013-06-21T13:02:50.127 回答