新手 - Yii2:无法显示来自控制器的视图数据

时间:2016-05-11 20:07:52

标签: php yii2

Yii2的新手,并从教程中学习一些演示,尝试复制代码并在我自己的演示中使用,但似乎无法工作。这是代码:

教程 - 控制器:

namespace app\controllers;
use yii\web\Controller;

use app\models\Article;
use Yii;

class ArticleController extends Controller{

    public function actionIndex(){

        $article = Article::find();

        //echo $article->count();

        $pagination = new \yii\data\Pagination(['totalCount'=>$article->count(), 'pageSize'=>3]);
        $data = $article->offset($pagination->offset)->limit($pagination->limit)->all();
        //print_r($data);

        return $this->render('index',['data' => $data, 'pagination' => $pagination]);
    }

教程 - 查看:

<?php

    use yii\helpers\Url;

?>
<p style = "text-align:right">
    <a href="<?=Url::to(['add'])?>" class="btn btn-primary">ADD</a>
</p>


<table class="table table-hover">
    <tr>
        <th>id</th><th>Title</th><th>Count</th><th>Status</th><th>Update Time</th><th>By</th>
    </tr>
    <?php print_r($data); foreach($data as $v){?>
    <tr>
        <td><?=$v->id?></td><td><?=$v->title?></td><td><?=$v->count?></td><td><?=($v->status == '1' ? 'Y' :'N')?></td>
        <td><?=date("Y-m-d H:i:s",$v->update_date)?></td>
        <td><a href="<?=Url::to(['edit', 'id'=>$v->id])?>">Edit</a> | <a href="<?=Url::to(['delete','id'=>$v->id])?>">Del</a></td>
    </tr>
    <?php }?>
</table>

<div style="float:right">
    <?=\yii\widgets\LinkPager::widget([
        'pagination' => $pagination,
        'options' =>[
            'class' =>'pagination',
            ],
        ])
    ?>
</div>

使用print_r($ data),它显示如下:

Array ( [0] => app\models\Article Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 1 [flag] => 0 [title] => 13123OOOO [description] => tttt1 [content] => tstsets 
[count] => 1 [status] => 1 [update_date] => 1462291610 [date] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 1 [flag] => 0 [title] => 13123OOOO 
[description] => tttt1 [content] => tstsets [count] => 1 [status] => 1 [update_date] => 1462291610 [date] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) 
[_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) 
[_behaviors:yii\base\Component:private] => Array ( ) ) 

但是根据我自己的代码: 控制器:

namespace app\controllers;
use yii\web\Controller;

use app\models\Inventory;

class InventoryController extends Controller{

    public function actionIndex(){

        //$inventory = new Inventory();
        $inventory = Inventory::find()->asArray()->all();
        print_r($inventory);

        return $this->renderpartial('index',['inventory' => $inventory]);
    }

查看:

<?php
use yii\helpers\Html;
use yii\helpers\Url;

echo "<br>";
print_r($inventory);
?>
<h2>Inventory Status</h2>
<table class = "table table-hover">
    <tr>
        <th>ID</th><th>Model</th><th>Office</th><th>Warehouse</th>
    </tr>
    <tr>
         <?php foreach($inventory as $item){ ?>
        <tr>
            <td><?=$item->id ?></td><td><?=$item->name ?></td><td><?=$item->office ?></td><td><?=$item->warehouse ?></td>
        </tr>
        <?php }?>
    </tr>
    <tr>
        <td>?<td>Apple</td><td>1</td><td>2</td>
    </tr>
</table>

我的print_r($ inventory)是这样的:

Array ( [0] => Array ( [id] => 1 [name] => Apple [office] => 1 [warehouse] => 2 ) [1] => Array ( [id] => 2 [name] => Android [office] => 2 [warehouse] => 1 ) ) 

并使用foreach(){}但无法显示任何数据,如何解决?

1 个答案:

答案 0 :(得分:0)

在你的代码中你有一个嵌套的tr(尝试删除)

<table class = "table table-hover">
    <tr>
        <th>ID</th><th>Model</th><th>Office</th><th>Warehouse</th>
    </tr>

         <?php foreach($inventory as $item){ ?>
        <tr>
            <td><?=$item['id'] ?></td><td><?=$item['name'] ?></td><td><?=$item['office'] ?></td><td><?=$item['warehouse'] ?></td>
        </tr>
        <?php }?>

    <tr>
        <td>?<td>Apple</td><td>1</td><td>2</td>
    </tr>
</table>