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 7607915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:47:17+00:00 2026-05-31T00:47:17+00:00

In cakephp, I retrieve data from mysql and assign them to an array but

  • 0

In cakephp, I retrieve data from mysql and assign them to an array but i am unable to reuse each element of it in the view.

In the controller i call the model to query data from the database, which have the following format

Array ([0]=>Array([user]=>Array([something]=>somevalue [somethingelse]=>someotherValue))
   [1]=>Array([user]=>Array([something]=>somevalue [somethingelse]=>someotherValue))
    .......)

and I initilize my ready-to-pass-to-view array as follows, $result is the array obtained from database.

$i=0;
foreach($result as $row)
{
   $exportDt[$i]['something']=$row['user'][something];
   $exportDt[$i]['somethingelse']=$row['user'][somethingelse];
}

this->set($exportDt);

How can i reuse this exportDt array in the view ? I am thinking that setting is allowed for only one dim array only.

  • 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-31T00:47:18+00:00Added an answer on May 31, 2026 at 12:47 am

    It appears you just need to use the correct syntax for setting the variable (ie passing the variable from the controller to the view):

    //controller
    $this->set('exportDt', $exportDt);
    
    //view
    print_r($exportDt);
    

    The first parameter for $this->set() is the name of the variable that you want accessible from the view. The second is the data to put into that variable.

    So, for example, you can even use other names:

    //controller
    $this->set('myVar', $exportDt);
    
    //view
    print_r($myVar);
    

    Another common practice is to use PHP’s compact. It looks for a variable by the name of the string(s), and creates an array with the name=>value

    …compact() looks for a variable with that name in
    the current symbol table and adds it to the output array such that the
    variable name becomes the key and the contents of the variable become
    the value for that key. In short, it does the opposite of extract().

    Example:

    //controller
    $this->set(compact('exportDt');
    
    //view
    $print_r($exportDt);
    

    More commonly, it’s used with multiple variables:

    //controller
    $var1 = 'whatever';
    $myVar = 'something else';
    $anotherVar = true;
    $this->set(compact('var1', 'myVar', 'anotherVar'));
    
    //view
    echo $var1. ' ' . $anotherVar . ' ' . $myVar;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the CakePHP stack in /var/www/site from one view under controller A I
CakePHP Newbie :) I am having trouble accessing another controller and passing that data
I have hosted my cakePhp project on hostgator. Its not able to retrieve data
I need to retrieve values from CakePHP's config file database.php from one of my
Using CakePHP 1.3 I'm trying to save some data to a MySQL database. The
I want to have cakephp paginate data and still use containable , but for
How to define and retrieve cookie value as array in CakePHP?
I am using cakephp containable to retrieve related models $this->Order->find('first', array( 'conditions'=>$conditions, 'contain' =>
I'm trying to retrieve the user id in a cakephp view, what's the best
I'm new to CakePHP but I've been though their FAQs and guides to no

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.