【问题标题】:Not able pass array to view in cakephp v 1.3无法传递数组以在 cakephp v 1.3 中查看
【发布时间】:2013-08-03 06:33:17
【问题描述】:

在我的 UsersController 的索引操作中,我想将字符串和数组传递给视图,以便我的视图正确呈现。我不知道我做错了什么,但我的视图文件没有让我的数组之一查看。

$users = $this->User->getUsers();

//Setting all variable that are present in layout
$this->set('title_for_layout','Users - Admin');
$page_headings = array("text" => "Users", "clss" => "icon-head");
$this->set('page_heading', $page_headings);
//Ends here

//This is actually going to action view file            
$this->set(compact('users'));

我的查看文件代码是这样的

<div><?php echo $page_heading[0]['text']; ?></div>

<?php foreach($users as $user){ ?>

我可以在这里获得 $users 变量,但注意到获得了 $page_heading。

我收到此错误Undefined variable: page_heading

我已经尝试过这样的一切:

$this->set($page_headings);

$this->set(compact('page_headings');

是的,在执行上述代码后,我已将视图文件中的变量名也更改为 page_headings。我无法让它工作。可以请任何人帮忙。

提前致谢。

【问题讨论】:

  • 而且title_for_layout 也可以正常提交吗?应该没什么区别,但是使用单个集合调用 $this->set(compact('title_for_layout', 'page_heading', 'users')); 怎么样?
  • ya.. title_for_layout 即将到来。我已经尝试将数组变量放在一起$this->set(compact('page_heading','users'));.. 现在我正在尝试您的建议.. 还有一件事我的$page_headings 在布局文件,不在动作的视图文件中..这有什么影响..??
  • @ndm 对不起,我对 title_for_layout 说错了。它也没有正确显示..在尝试了您建议的代码后,它显示 users 和 page_heading 都是未定义的,也没有显示页面标题..
  • 这听起来很奇怪...您的确切 CakePHP 版本是什么?你在布局/视图渲染方面做了什么特别的事情吗?可能是自定义视图类之类的东西?您是否尝试过使用全新的默认 CakePHP 安装是否会出现问题?
  • 我正在开发 1.3.17 版本的 cakePhp。不,我没有做任何特别的事情。也许我应该尝试全新安装。

标签: cakephp cakephp-1.3


【解决方案1】:

发生这种情况是因为我直接从 filezilla 的编辑选项编辑我的实时服务器文件,当我在编辑文件后保存它时,它不知何故变成了一行代码,并且由于注释标签,我编辑的所有代码都变成了厘米。

当我从服务器获取新副本时,我纠正了这个问题。

所以我今天学到了什么:说在文件上工作了大约半小时后,如果您从 filezilla 编辑,请从服务器获取更新的文件,因为您不知道它将如何保存文件。

感谢ndm 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2016-07-27
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多