【问题标题】:How do I troubleshoot an "Undefined variable" notice in PHP?如何解决 PHP 中的“未定义变量”通知?
【发布时间】:2023-03-18 07:20:01
【问题描述】:

我有两个模型:“画廊”和“图像”。当我尝试从 gallery_controller 获取图像时,我看到了这个错误:

注意(8):未定义变量:images [APP\controllers\galleries_controller.php,第 25 行]

我使用 CakePHP 的 find 方法来限制图像结果。

这是我的画廊控制器:

function view($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid image', true));
        $this->redirect(array('action' => 'index'));
    }
    $this->set('gallery', $this->Galley->read(null, $id));
    /// this code have some problem ///
    $this->loadModel('image');
    $Images=  $this->Image->find('all',
        array(
            'limit' => 2, //int
        )
    );
    $this->set('images', $images);
}

【问题讨论】:

  • $this->loadModel('image'); 是第 25 行吗?
  • $this->set('images', $images);在第 25 行

标签: php cakephp-1.3


【解决方案1】:

变量名区分大小写($Images和$images需要全小写或全大写)

// you have a UPPERCASE I    
$Images = $this->Image->find([...]

// you have a lowercase I
$this->set('images', $images);

【讨论】:

  • 谢谢 gabler..请再问一个问题..除了这个方法之外,您还有什么方法可以找到限制图像,或者这是最好的方法
  • 不知道,抱歉。我不再使用 CakePHP,现在我正在学习 Zend Framework 2 =)
【解决方案2】:

检查您的大小写...

$Images=  $this->Image->find('all',
...
$this->set('images', $images);

$Images != $images

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2014-07-17
    • 2018-05-06
    相关资源
    最近更新 更多