php - Kohana View - 循环 find_all()

标签 php mysql kohana

我是一个非常新的尝试一些 kohana 代码的人,并且已经遇到了第一个问题:(

为了快速起见,这是我的模型

<?php

class Model_Post extends ORM {

}

这是我的 Controller

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Blog extends Controller {

    public function action_index()
    {
        $posts = ORM::factory('Post')->find_all();
        $view = View::factory('blog/index')
            ->bind('posts', $posts); 
        $this->response->body($view);
    }
} // End Blog

这是我的观点

<h2>My list of blog posts</h2>
<? foreach($posts as $post): ?>
    <hr />
    <h4><?= $post->author ?></h4>
    <p><?= $post->body ?></p>
<?endforeach; ?>

现在我收到错误

ErrorException [ Notice ]: Undefined variable: post
APPPATH\views\blog\index.php [ 4 ]
1 <h2>My list of blog posts</h2>
2 <? forech($posts as $post): ?>
3   <hr />
4   <h4><?= $post->author ?></h4>
5   <p><?= $post->body ?></p>
6 <?endforeach; ?>

我正在经历this video

当我将 Controller 更改为时,我的结果集不为空

class Controller_Blog extends Controller {

    public function action_index()
    {
        $posts = ORM::factory('Post')->find_all();
    //  $view = View::factory('blog/index')
    //      ->bind('posts', $posts); 
        $this->response->body($posts[0]->body);
    }
} // End Blog

它显示了我的第一行

最佳答案

我也有同样的问题!我尝试像这样更改index.php:

<h2>My list of blog posts</h2>

<?php foreach($posts as $post): ?>
    <hr />
    <h4><?php echo $post->author ?></h4>
    <p><?php echo $post->body ?></p>
<?php endforeach; ?>

它对我有用!

关于php - Kohana View - 循环 find_all(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23365779/

相关文章:

javascript - 从 mySql 数据库在 php 中返回 JSON

php - Codeigniter Controller /模型名称冲突

sql - GRANT 语句中的通配符?

php - 如何处理访问一个 'brain' 的多个站点?

php - 如何合并/消除类文件的 PHP 包含

php - 将 Twig 与 react.js 集成

php - 正则表达式作为抵御 XSS 的第一道防线

php - 使用 Kohana Request 时如何设置 CURL 选项

php - 关闭PHP代码的正确形式,自由结果并关闭连接

MySQL 获取不返回值