Yii如何预处理数据库中的数据


Yii How to foreach data from DB

如何从数据库中获取数据?一个开发者有多个游戏,但当我从数据库中调出数据时,它只显示一个数据

以下是我的编码:

在控制器中

$id = Yii::app()->user->getState('id');
$thedb = GamesDevelopersApp::model()->find('developer_id='.$id);
$gametitle     = CHtml::encode($thedb->gametitle);
$version       = CHtml::encode($thedb->version);
$create_time   = CHtml::encode($thedb->uploaddate);
$status        = CHtml::encode($thedb->status);
$this->render('applist',array('gametitle'=>$gametitle,'version'=>$version,'create_time'=>$create_time,'status'=>$status));

在HTML中

    <td class="apptd2">
        <?php foreach($models as $model){
            echo CHTML::encode($model->gametitle);  
        }; ?>
    </td>

在DB请求中,您只检索了一个条目,因为您使用的是find方法:

$thedb = GamesDevelopersApp::model()->find('developer_id='.$id);

在Yii文档中,您将看到find方法:

查找具有指定条件的单个活动记录。

这就是为什么以下循环没有感应的原因

<?php foreach($gametitle as $title){
   echo $title;  
 }; ?>

对我来说,最好在控制器中使用findAll

$id = Yii::app()->user->getState('id');
$models = GamesDevelopersApp::model()->findAll('developer_id='.$id);
$this->render('applist',array('models'=>$models));

视图中的以下循环:

<?php foreach($models as $model){
   echo CHTML::encode($model->gametitle);  
 }; ?>