title: Foreach logic issue with CakePHP
body: <p>I have I problem that I hope someone can help me with. I thought this code was right, but it will not work. Below is my code, it is a function for my CakePHP 2.2.2 site, the main aim of the code is to produce a menu system from database results. The problem is with my foreach loop, it will not loop. All $Key does is return the value of 2 (three records within the table at this time). So When I display / echo / debug $Menu, the only result I get is the last result stored within the database. </p>
  
  <p>I know the SQL command is right, if that is debuged / echoed then all three results are displayed. The idea of this loop was to get it to count the results, so that I could run a check on a selected field. Where I am going wrong?</p>
  
  <pre><code>function MenuSystem() {
   $this-&gt;loadModel('Menu');
  
   $MenuSQL = $this-&gt;Menu-&gt;find('all', array('conditions' =&gt; array('active' =&gt; true)));
  
   foreach ($MenuSQL as $Key=&gt;$Value) {
  
      $MenuSystem = $MenuSQL[$Key];
      $this-&gt;Menu = $MenuSystem;
  
   }
  
   }
  </code></pre>
  
  <p>Many Thanks,
  Glenn.</p>
  
  <p>UPDATE :::</p>
  
  <p>Below is my function, now my foreach loop now works, don't know what I was doing wrong, but I know think its working. You can see the print_r command that I am using for testing, if I use that, then all links from my database are printed / echoed on the screen and all works. But if I try and call the $this->Menu from another controller, then only the last record is echoed on the screen. I have moved the $this->Menu outside of the foreach loop, but it made no difference, with it inside the loop or outside, it still only echoes the last record and not all three. So what I am doing wrong?</p>
  
  <pre><code>function MenuSystem() {
    $this-&gt;loadModel('Menu');
    $SiteBase = '/projects/cake/';
    $MenuSQL = $this-&gt;Menu-&gt;find('all', array('conditions' =&gt; array('active' =&gt; true)));
  
  foreach ($MenuSQL as $key =&gt; $Value) {
  
      $MenuAccessLevel = $MenuSQL[$key]['Menu']['roles_id'];
  
      if ($MenuAccessLevel == 1) {
        $Path = $MenuSQL[$key]['Menu']['path'];
        $Title = $MenuSQL[$key]['Menu']['title'];
        $MenuSys = "&lt;a href=\" " . $SiteBase . $Path . " \"&gt;" . $Title ."";
      } else {
         print ("Admin");
      }
      //print_r($MenuSys);
  } //End of Foreach Loop 
     $this-&gt;Menu = $MenuSys;
  } //End of function MenuSystem
  </code></pre>