dongyin2643 2016-06-21 18:03
浏览 63

如何创建表单选择制作值与db数组中的选项文本相同

Let's say I have in my controller:

$this->set('locations', $this->Location->find('list', array('fields' => array('state')) )); 

How do I get my view to generate a form select with options:

<select name="data[Location][state]" class="inputbox" id="Location">
<option value="Arizona">Arizona</option>
<option value="California">California</option>
<option value="Hawaii">Hawaii</option>
</select>

Because it is generating:

<select name="data[Location][state]" class="inputbox" id="Location">
    <option value="1">Arizona</option>
    <option value="2">California</option>
    <option value="3">Hawaii</option>
    </select>

I'm aware that cakephp uses primary key by default for the values and there's an answered question but the solution did not work for me: how do you create a select with both value and the label the same from an array in cakephp?

In my view, I have:

<?php echo $form->select('Location.state', $locations); ?>

My database is: locations
id | state
1 | Arizona
2 | California
3 | Hawaii
and so on

  • 写回答

3条回答 默认 最新

  • dslf46995 2016-06-21 18:15
    关注

    alright, solution was pretty simple...in my controller the fields array needed the value and text specified:

    $this->set('locations', $this->Location->find('list', array('fields' => array('state','state')) ));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作