gpt4 book ai didi

php - 如何修复cakePHP中的Notice (8) : Undefined variable: books [APP/Template\books\index. ctp, line 6]?

转载 作者:行者123 更新时间:2023-11-29 10:49:36 25 4
gpt4 key购买 nike

我完全是使用 CakePhp 的新手。我已经解决了一些问题,但我又对这个基本问题了如指掌。你能帮我解决这个问题吗?

Notice (8): Undefined variable: books [APP/Template\books\index.ctp, line 6]
Warning (2): Invalid argument supplied for foreach() [APP/Template\books\index.ctp, line 6]

这是我的代码,错误消息指出:

<table>
<thead>
<th>ISBN</th><th>Title</th><th>Author</th>
</thead>
<?php foreach($books as $book): ?>
<tr>
<td><?php echo $book['books']['isbn'] ?></td>
<td><?php echo $book['books']['title'] ?></td>
<td><?php echo $book['authors']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>

这是我的 BooksController.php:

<?php
/**
* @property BooksController $BooksController
*/
namespace App\Controller;
use App\Controller\AppController;
class BooksController extends AppController {
public function display()
{
function index() {
$this->books->recursive = 1;
$books = $this->books->find('all');
$this->set('books', $books);
}
$this->render('index');
}
}
?>

这是我的 books.php:

<?php
/**
* @property books $books
*/

class books extends AppModel
{
var $name = 'books';
var $belongsTo = 'authors';
}
?>

最佳答案

您需要像这样从显示方法中获取索引方法

<?php
/**
* @property BooksController $BooksController
*/
namespace App\Controller;
use App\Controller\AppController;
class BooksController extends AppController {
public function display()
{
}
function index() {
$this->books->recursive = 1;
$books = $this->books->find('all');
$this->set('books', $books);
$this->render('index');
}
}
?>

关于php - 如何修复cakePHP中的Notice (8) : Undefined variable: books [APP/Template\books\index. ctp, line 6]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43972227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com