I have a list of Countries that I retrieve from my database with a find('list') and I want to add a separator after the 4th element – it's a list of Countries, the first four are the most popular, then I want the separator, and then the remainder are simply in alphabetical order. I can't seem to find a good way to do this. Any suggestions?
Thanks.
One option is to use "optgroup". Cakephp form helper will generate that, if your array has 2 levels.
$countries = $this->Country->find('list'); // based on your implementation
$popularCountries = array();
$otherCountries = array();
foreach ($countries as $countryId => $countryName) {
if ($countryId < 5) { // this might also depend on the IDs, or how you determine if a country is popular or not
$popularCountries[$countryId] = $countryName;
} else {
$otherCountries[$countryId] = $countryName;
}
}
$options = array(
'Popular countries' => $popularCountries,
'Other' => $otherCountries
);
Then use
echo $this->Form->input('country', array('type' => 'select', 'options' => $options));
You could also just insert a country with no value, and name equal to '-------' after the 4th element from your list.