June 9, 2006

A Radio List in a table for CakePHP

CakePHP — Sarah King

I wanted to present a range of choices in a nicely formatted table and couldn't see helper to do the job - so this is my first snippet for Cake PHP. I doubt it's perfectly cake (for instance I do the header row myself rather than using the html helper, and I concatenate a string rather than building an array)

In this instance I added the function radioTable to a helper called "fed" and gave it the standard model and field variables.

To call it I just use

PHP:
  1. echo $fed->radioTable('Call/mtype', array('','', 'Joining Fee', 'Annual Fee'), $mtypevals);

where the second parameter is the nice labels for my table.

PHP:
  1. function radioTable($fieldName, $headers, $options, $htmlAttributes=null,$return=false)
  2.     {
  3.         $this->setFormTag($fieldName);
  4.         $value = (isset($htmlAttributes['value']))? $htmlAttributes['value']: $this->Html->tagValue($fieldName);
  5.      $out = '<table><tbody>';
  6.      if (is_array($headers))
  7.      {
  8.         $out .= '<tr>';
  9.         foreach($headers as $th) $out .= "<th>{$th}</th>\n";
  10.         $out .= '</tr>';
  11.      }
  12.      $thisTag = "<tr><td>\n{$this->Html->tags['radio']}\n</td>\n";
  13.        
  14.      foreach ($options as $holder)
  15.      {         
  16.         if (count($holder)>1)
  17.         {
  18.             $thisItem = $thisHolder = array();
  19.             foreach($holder as $toMerge) $thisItem = array_merge($thisItem, $toMerge);
  20.             $thisHolder[] = $thisItem;
  21.         }
  22.         else $thisHolder = $holder;
  23.            
  24.         foreach ($thisHolder as $item)
  25.         {
  26.             $keys = array_keys($item);
  27.             if (is_array($item[$keys[0]]))
  28.             {
  29.                 $thisItem = array();
  30.                 foreach($item as $toMerge)  array_merge($thisItem, $toMerge);
  31.             // will have changed so need to rework
  32.                $keys = array_keys($thisItem);
  33.             }
  34.             else $thisItem = $item;
  35.                
  36.             $optValue = $thisItem[$keys[0]];
  37.             $optionsHere = array('value' => $optValue);
  38.             $optValue==$value? $optionsHere['checked'] = 'checked': null;
  39.             $parsedOptions = $this->Html->parseHtmlOptions(array_merge($htmlAttributes, $optionsHere), null, '', ' ');
  40.             $individualTagName = "{$this->field}_{$optValue}";
  41.             $out .= sprintf($thisTag, $this->model, $this->field, $individualTagName, $parsedOptions, '');
  42.             $cnt = count($item);
  43.                 
  44.             //start on the second element in the array
  45.             for($i=1; $i <$cnt; $i++) $out .= "<td><label for='{$individualTagName}'>{$item[$keys[$i]]}</label>\n";
  46.            $out .= '</tr>';
  47.         }
  48.      }
  49.      $out .= '</tbody></table>';
  50.  
  51.      return $this->output($out? $out: null, $return);
  52. }//radioTable

Also on this site

Tags: ,

1 Comment »

  1. how you use this example I can not set it up

    Comment by isaac — July 3, 2006 @ 9:17 am

Leave a comment

RSS feed for comments on this post. TrackBack URI

Search

Pages

Categories:

Other Resources

Subscribe in NewsGator Online
GeoURL