It took me a while to figure out the dropdown equivalent on CakePHP framework, and finally realized its as simple as creating, probably a text box or even a button. Here is how it’s done:

“select” is the keyword used to create the dropdown, here is the syntax from CakePHP help docs :

select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element, populated with the items in $options, with the option specified by $selected shown as selected by default. Set $showEmpty to false if you do not want an empty select option to be displayed.

$options=array(‘M’=>’Male’,’F’=>’Female’);
echo $form->select(‘gender’,$options)
?>

Will output:


<select name="data[User][gender]" id="UserGender">

<option value=""></option>

<option value="M">Male</option>

<option value="F">Female</option>

</select>


What you will observe in the dropdown list are the 2 options, Male and Female. However, after persisting this to the database, your table will contain ‘M’ or ‘F’ depending on your selection. This is because while declaring the parameter $options, you assigned ‘M’=>’Male’, ‘F’=>’Female’.

That’s it, you can modify the above snippet as per your requirements and you are good to go!!

Advertisements