A Radio List in a table for CakePHP

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]echo $fed->radioTable(‘Call/mtype’, array(”,”, ‘Joining Fee’, ‘Annual Fee’), $mtypevals);[/php]

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

[php]function radioTable($fieldName, $headers, $options, $htmlAttributes=null,$return=false)
{
$this->setFormTag($fieldName);
$value = (isset($htmlAttributes[‘value’]))? $htmlAttributes[‘value’]: $this->Html->tagValue($fieldName);
$out = ‘

‘;
if (is_array($headers))
{
$out .= ‘

‘;
foreach($headers as $th) $out .= “

\n”;
$out .= ‘

‘;
}
$thisTag = “

\n”;

foreach ($options as $holder)
{
if (count($holder)>1)
{
$thisItem = $thisHolder = array();
foreach($holder as $toMerge) $thisItem = array_merge($thisItem, $toMerge);
$thisHolder[] = $thisItem;
}
else $thisHolder = $holder;

foreach ($thisHolder as $item)
{
$keys = array_keys($item);
if (is_array($item[$keys[0]]))
{
$thisItem = array();
foreach($item as $toMerge) array_merge($thisItem, $toMerge);
// will have changed so need to rework
$keys = array_keys($thisItem);
}
else $thisItem = $item;

$optValue = $thisItem[$keys[0]];
$optionsHere = array(‘value’ => $optValue);
$optValue==$value? $optionsHere[‘checked’] = ‘checked’: null;
$parsedOptions = $this->Html->parseHtmlOptions(array_merge($htmlAttributes, $optionsHere), null, ”, ‘ ‘);
$individualTagName = “{$this->field}_{$optValue}”;
$out .= sprintf($thisTag, $this->model, $this->field, $individualTagName, $parsedOptions, ”);
$cnt = count($item);

//start on the second element in the array
for($i=1; $i < $cnt; $i++) $out .= "

‘;
}
}
$out .= ‘

{$th}
\n{$this->Html->tags[‘radio’]}\n \n”;
$out .= ‘

‘;

return $this->output($out? $out: null, $return);
}//radioTable
[/php]

Categories

Recent Comments

Tags

One Comment

  1. isaac
    July 3, 2006

    how you use this example I can not set it up

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.