【问题标题】:render actionIndex() SQL query so available in other view file in Yii渲染 actionIndex() SQL 查询,以便在 Yii 的其他视图文件中可用
【发布时间】:2011-12-30 22:49:57
【问题描述】:

我刚刚开始使用 Yii,我只是在创建我的主页,我为这个文件使用了两个视图,index.php 和 column1.php。我的 SiteController.php 中有以下代码

public function actionIndex()
{

// limits query for index page results to 10
$Criteria = new CDbCriteria();
$Criteria->limit = 10;
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->render('index', array(
                'ItemDetails' => ItemDetail::model()->findAll($Criteria),
        ));
}

这只是回忆我想在我的主页上显示的最新 10 条记录。要访问我的 index.php 文件中的记录,我只需使用

<?php foreach($ItemDetails as $ItemDetail):?>
<tr>
    <td><?php echo $ItemDetail->title;?></td>
    <td><?php echo $ItemDetail->des;?></td>
</tr>
<?php endforeach;?>

唯一的问题是我需要将此代码输出到我的 column1.php 视图。我如何实现这一点?目前我只是得到未定义的变量,或者我应该把所有东西都放在我的 index.php 视图中 - 我有点不确定,所以希望得到一些指导

谢谢

强尼

【问题讨论】:

    标签: yii


    【解决方案1】:

    首先,colum1 是一个布局而不是一个视图,你可以通过在函数 actionIndex 的开头设置布局来实现,就像这样$this-&gt;layout='column1' 也许您需要部分渲染。 basics view layout layout detail 查看渲染部分的博客示例

    【讨论】:

    • 嗨 cherif_b,是的,你是对的,我使用 layout='column1'; 在 index.php 视图中包含 column1 布局?> 在我的索引视图中,这是我需要能够返回 sql 数据的地方 - 任何进一步的想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 2015-04-08
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多