title: CakePHP if else to display an ARRAY or FIRST (one row) or NULL php echo
body: <p>I've got a table, that at any point may have NOTHING to display, ONE row or MULTIPLE rows.</p>
  
  <p>In my first go I set the variable as an ARRAY finding ALL, but obviously if there's 1 row or 0 rows CakePHP throws a INVALID ARGUMENT SUPPLIED FOR FOREACH. I could set the ARRAY finding FIRST, but if there's more than 1 row it will only display the first.</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p>The function in the Field Controller:</p>
  
  <pre><code>public function add($id){
  
      //Set Title, Stylesheet, homelogo &amp; layout  
      $this-&gt;set('title_for_layout', 'View Invoices');
      $this-&gt;set('stylesheet_used', 'homestyle');
      $this-&gt;set('image_used', 'eBOXLogoHome.png');   
      $this-&gt;layout='home_layout';
  
      //Find all fields where template_id = template_id passed in
      //ARRAY: to print foreach loop ONLY WORK &gt;2 Row returned
      $templatefields=$this-&gt;Field-&gt;find('all', array(
      'conditions' =&gt; array(
      'Field.template_id' =&gt; $id)));
  
      //Find all fields where template_id = template_id passed in
      //FIRST: to print echo variable ONLY WORK =1 Row returned
      //$templatefields=$this-&gt;Field-&gt;find('first', array(
      //'conditions' =&gt; array(
      //'Field.template_id' =&gt; $id)));
  
      //Set variables
      $this-&gt;set('templatefields', $templatefields); 
  }
  </code></pre>
  
  <p>$id that is passed in is the Template id that the fields belong to.</p>
  
  <p>The display function in Fields/add.ctp.</p>
  
  <pre><code>&lt;?php
  if (is_array($templatefields))
  {
      foreach ($templatefields as $templatefield)
      {
          &lt;tr&gt;
              &lt;td align='center'&gt;&lt;?php echo $templatefields['Field']['name']; ?&gt;&lt;/td&gt;
              &lt;td align='center'&gt;&lt;?php echo $templatefields['Field']['description']; ?&gt;&lt;/td&gt;
              &lt;td align='center'&gt;&lt;?php echo $templatefields['Field']['default_value']; ?&gt;&lt;/td&gt;
              &lt;td align='center'&gt;&lt;?php echo $templatefields['Field']['template_id']; ?&gt;&lt;/td&gt;
          &lt;/tr&gt;
      }
  }
  else if (count($templatefields)==1)
  {
      ....print? --&gt; WOULD HAVE TO USE $templatefields where find('first') 
      ....otherwise error=invalid argument supplied for foreach
  }
  else if (empty($templatefields))
  {
      ....NULL
      ....OR "There are no fields to display"
  }
  ?&gt;
  </code></pre>