【问题标题】:Fetch database record values in view file in cakephp在 cakephp 的视图文件中获取数据库记录值
【发布时间】:2013-11-26 23:50:58
【问题描述】:

作为我的联接查询的结果,我有一个记录数组。

我想在我的视图文件中显示所有完整记录。这怎么可能?我是 cakephp 新手。

我的控制器功能

function viewsurvey()
{
    $sid = $this->params['url']['sid'];
    $this->set('sid',$sid);
    $this->Survey->create();
    $this->loadModel('Question');
     $quest = $this->Question->find('all', array('joins' => array( 
    array( 
        'table' => 'surveys', 
        'alias' => 'Survey', 
        'type' => 'inner', 
        'foreignKey' => false, 
        'conditions'=> array('Survey.id = Question.survid',
            'Question.survid' => $sid) 
    )

        ))); 

        print_r($quest)

        // The record is Array ( [0] => Array ( [Question] => Array ( [id] => 9 [survid] => 2 [quest] => mnjlkj [type] => 2 [qvalues] => yes,no ) ) [1] => Array ( [Question] => Array ( [id] => 10 [survid] => 2 [quest] => mnm,n [type] => 1 [qvalues] => 0 ) ) )

}

我希望在我的视图文件中获取 div 中的每一行;直到获取所有行。这怎么可能。希望有人能尽早给我建议。

【问题讨论】:

    标签: cakephp cakephp-2.1


    【解决方案1】:

    您需要在控制器中设置变量$quest。所以view.ctp可以访问数据

    $this->set('quest', $quest);
    

    而且你可以在你的视图中阅读它。

    foreach($quest as $q){
       echo '<div>'.$q['Question']['id'].'<br />
          '.$q['Question']['survid'].'<br />
          '.$q['Question']['quest'].'<br />
          '.$q['Question']['type'].'<br />
          '.$q['Question']['qvalues'].'
       </div>';  
    }
    

    对于您拥有的每条记录,都会创建一个新的 div。在您的情况下,将制作 2 个 div

    在评论循环数字后编辑:

    $i=1;
    foreach($quest as $q){
       ...
       ...
       $i++;
    }
    

    【讨论】:

    • 你能给每个循环一个像 1、2 等的数字吗?这怎么可能?
    • 你是什么意思?你的意思是像在编辑下?它会为您计算所有循环吗?从 1 开始?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多