如何在controller中存储数组并在cakephp的视图文件中使用

发布于 2024-08-04 01:24:01 字数 1237 浏览 11 评论 0原文

在我的用户控制器中,我通过查询“表单”表找到用户创建的表单数量并将其存储在数组中。但是如何在视图文件中使用数组变量。

通常,我会使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-08-11 01:24:01

首先,您应该避免重复调用函数。

函数的前两行可以替换为:

$users=$this->User->find('all');
$this->set('users', $users);

接下来,您可以在将对象传递给视图之前更改对象并将您正在计算的属性添加到该对象。

function users(){

    $users=$this->User->find('all');

    // notice the & here
    foreach($users as & $user):

       // you are repeating the same line here as well, that's not necessary
       $user['User']['custom_field'] = $this->Form->find('count', 
           array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
    endforeach;

    $this->set('users', $users);
}

然后您可以像从数据库中获取的任何其他字段一样在视图中使用它。为了进一步改进它,您可能需要考虑将此代码移动到 User 模型中的 afterFind 回调中,至少在多次使用此自定义值的情况下是如此。

First of all you should avoid repeating your function calls.

First two lines of your function can be replaced by:

$users=$this->User->find('all');
$this->set('users', $users);

Next you can alter your object before passing it to view and add the property you are calculating to that object.

function users(){

    $users=$this->User->find('all');

    // notice the & here
    foreach($users as & $user):

       // you are repeating the same line here as well, that's not necessary
       $user['User']['custom_field'] = $this->Form->find('count', 
           array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
    endforeach;

    $this->set('users', $users);
}

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 your User model, at least if this custom values are used more then once.

聆听风音 2024-08-11 01:24:01

您已经知道如何设置数组值:-)

在这段代码中,您正在设置数组“users”,

$this->set('users',$this->User->find('all'));

因此您可以对另一个数组执行相同的操作:

$this->set('userFormCount',$userFormCount);

...并以与 users 变量相同的方式读取它。

但请看看 RaYell 的答案,因为它解释了如何使您当前的代码更好。

You already know how to set an array value :-)

In this code you are setting the array 'users'

$this->set('users',$this->User->find('all'));

So you can do the same for the other array:

$this->set('userFormCount',$userFormCount);

...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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文