简体   繁体   English

如何设置从数据库表中获取的值作为cakephp下拉列表中的选定值?

[英]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"=>a‌​rray("city1"=>"city1‌​","city2"=>"city2","‌​city3"=>"city3","cit‌​y4"=>"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","cit‌​y4"=>"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.

相关问题 如何将下拉列表中的其他选定值插入数据库 - How to insert different selected value from drop down list into database 如何使用下拉列表中的选定值显示数据库中的列表 - How to display the list from database using selected value in drop down 如何将选择的值从下拉列表传递到数据库表 - How to pass chosen value from drop down list to the database table 从MySQL数据库中预先选择的下拉列表值 - Pre-selected drop down list value from MySQL database 如何在下拉列表上单击时将PHP会话值设置为选定的值 - How to set a PHP session value as selected onclick on a drop down list 如何使用php或javascript在下拉菜单中使从mysql获取的值作为选定选项 - how to make the value fetched from mysql as selected option in a drop down using php or javascript 如何从下拉选择框列表中将用户选择的选项的值存储到mysql数据库中? - How can I store the value of a user selected option from a drop down select box list into mysql database? 如何从下拉列表中获取所选值 - How can I get the selected value from a drop down list 如何将选定的值从下拉列表保存到php中的mysql - how to save selected value from drop down list to mysql in php 根据数据库获取的值设置所选值 - Set Selected Value Based On Database Fetched Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM