Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 359969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:26:38+00:00 2026-05-12T12:26:38+00:00

In my Users controller, I find the number of forms that the user has

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:26:38+00:00Added an answer on May 12, 2026 at 12:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entries controller that allows users to add contact information the website.
I have a controller action that checks this.User.Identity.IsAuthenticated What do you suggest how to
I wrote a device controller (rs232) and it is being used successfully, however users
I have a form which users must fill out and submit. The controller action
I have a controller user with a method login For some reason, when I
I have a simple UserControl for database paging, that uses a controller to perform
Currently my controller lets a user submit muliple links at a time. It collects
I know there are actually a number of questions similar to this one, but
I have a really simple Rails application that allows users to register their attendance
I have a number of functions in my controller logic and for every one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.