如何在controller中存储数组并在cakephp的视图文件中使用
在我的用户控制器中,我通过查询“表单”表找到用户创建的表单数量并将其存储在数组中。但是如何在视图文件中使用数组变量。
通常,我会使用 set 函数在控制器中设置变量,并在视图文件中使用它。但是如何在控制器中设置数组呢?我得到了控制器中的值(做了回声)。
我的控制器代码是:
function users(){
$this->set('users',$this->User->find('all'));
$users=$this->User->find('all');
foreach($users as $user):
$id=$user['User']['id'];
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
echo "users : ".$id." ".$userFormCount[$id];
endforeach;
}
我的视图文件:
<?php foreach($users as $user):?>
<tr>
<td class="people_profile">
<a href="/users/angeline"><?php echo $user['User']['name'];?></a>
</td>
<td>
<?php
$id=$user['User']['id'];
echo $userFormCount[$id]." ";?>forms, //need the array value here.
0 reports,
0 badges
</td>
</tr>
<?php endforeach;?>
由于我已将值存储在控制器中的变量 $userFormCount 中,因此我没有在视图文件中获取该值。但如何设置和获取数组值呢?
In my Users controller, I find the number of forms that the user has created by querying the 'Forms' table and store it in an array. But how to use the array variables in the view file.
Normally, I would set the variable in the controller using set function and use it in the view file as such. But how to set an array in the controller? I get the values in the controller(did an echo).
My controller code is:
function users(){
$this->set('users',$this->User->find('all'));
$users=$this->User->find('all');
foreach($users as $user):
$id=$user['User']['id'];
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
echo "users : ".$id." ".$userFormCount[$id];
endforeach;
}
My view file:
<?php foreach($users as $user):?>
<tr>
<td class="people_profile">
<a href="/users/angeline"><?php echo $user['User']['name'];?></a>
</td>
<td>
<?php
$id=$user['User']['id'];
echo $userFormCount[$id]." ";?>forms, //need the array value here.
0 reports,
0 badges
</td>
</tr>
<?php endforeach;?>
Since I have stored the value in a varaible $userFormCount in the controller, I do not get the value in the view file. but how to set and get an array value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该避免重复调用函数。
函数的前两行可以替换为:
接下来,您可以在将对象传递给视图之前更改对象并将您正在计算的属性添加到该对象。
然后您可以像从数据库中获取的任何其他字段一样在视图中使用它。为了进一步改进它,您可能需要考虑将此代码移动到
User
模型中的afterFind
回调中,至少在多次使用此自定义值的情况下是如此。First of all you should avoid repeating your function calls.
First two lines of your function can be replaced by:
Next you can alter your object before passing it to view and add the property you are calculating to that object.
and you can then use it in your view like any other field that you get from the database. To improve it even firther you may want to consider moving this code to a
afterFind
callback in yourUser
model, at least if this custom values are used more then once.您已经知道如何设置数组值:-)
在这段代码中,您正在设置数组“users”,
因此您可以对另一个数组执行相同的操作:
...并以与 users 变量相同的方式读取它。
但请看看 RaYell 的答案,因为它解释了如何使您当前的代码更好。
You already know how to set an array value :-)
In this code you are setting the array 'users'
So you can do the same for the other array:
...and read it in the same way as your users variable.
But please look at RaYell's answer, because it explains how to make your current code better.