2012-03-06 04:35
浏览 23


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.

foreach($result as $row)


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

图片转代码服务由CSDN问答提供 功能建议

在cakephp中,我从mysql中检索数据并将它们分配给一个数组,但我无法重用它的每个元素 在视图中。


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

我启动了我的准备传递给 -view数组如下,$ result是从数据库获得的数组。

  $ i = 0; 
foreach($ result as $ row)
 $ exportDt  [$ i] ['something'] = $ row ['user'] [something]; 
 $ exportDt [$ i] ['somethingelse'] = $ row ['user'] [somethingelse]; 
} \  n 
this-> set($ exportDt); 

如何在视图中重用此exportDt数组? 我想只允许一个dim数组的设置。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyan4157 2012-03-06 04:42

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

    $this->set('exportDt', $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:

    $this->set('myVar', $exportDt);

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



    More commonly, it's used with multiple variables:

    $var1 = 'whatever';
    $myVar = 'something else';
    $anotherVar = true;
    $this->set(compact('var1', 'myVar', 'anotherVar'));
    echo $var1. ' ' . $anotherVar . ' ' . $myVar;
    解决 无用
    打赏 举报

相关推荐 更多相似问题