[英]How to set a value fetched from database table as selected value in drop down list in cakephp?
Hey everyone I am trying to set selected="selected" for drop down list but cant seem to get to solution. 嘿,我正在尝试为下拉列表设置selected =“ selected”,但似乎无法解决。
Eg. 例如。 A city is entered in user's table when user chooses to edit his profile the rest of the details comes in text boxes but the city should be displayed in drop down list and the default selected value should match with the city of that user.
当用户选择编辑个人资料时,会在用户表中输入城市,其余详细信息会显示在文本框中,但城市应显示在下拉列表中,默认选择的值应与该用户的城市匹配。
Note :- The citites are entered directly in like this 注意:-像这样直接输入引文
echo $this->Form->input("city",array("type"=>"select","empty"=>"City","options"=>array("city1"=>"city1","city2"=>"city2","city3"=>"city3","city4"=>"city4")));
You can use default
to make any option as selected in cakephp
您可以使用
default
来使cakephp
选择的任何选项
Try This: 尝试这个:
echo $this->Form->input('city', array('type' => 'select', 'options' => array("city1"=>"city1","city2"=>"city2","city3"=>"city3","city4"=>"city4"), "default" => "city1"));
You don't need to do anything for selected value. 您无需为选定的值做任何事情。 Cakephp render automatically fields name.
Cakephp会自动呈现字段名称。 so if in the user user table city field name is
city_id
than you need to write below statement: 因此,如果在用户用户表中city字段名称为
city_id
,则需要编写以下语句:
<?php echo $this->Form->input('city_id', array("label" => false, 'type' => 'select', 'options' => $cities, "empty"=>"Select City", "div" => false )); ?>
But if your data is not rendered from cities table, then you need to write the same column name of the select box: 但是,如果您的数据不是从“城市”表中呈现的,那么您需要编写与选择框相同的列名:
<select name="city">
<?php foreach ($city_array as $key => $value) { ?>
<option <?php echo ($value == $default) ? 'selected' : '' ?> value="<?php $value ?>"><?php echo $value</option>
<?php } ?>
</select>
The third option to get select value like below: 第三种获取选择值的选项,如下所示:
<?php echo $this->Form->input('city', array(
'type' => 'select',
'options' => array('city1' => 'city1', 'city2' => 'city2', 'city3' => 'city3'),
'selected' => 'city1'
));
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.