dswg47377 2015-03-25 12:56
浏览 65
已采纳

在CakePHP中将prename和lastname显示为select选项

I want to show a form with CakePHP where users can select an employee. Cake shows the inputfield as "select" which is good. Unfortunately the select input field only shows the id of the employee rather than the prename and lastname.

Model:

Reminder:

worker_id (reference to Worker model)

Worker:

id
prename
lastname

Here is the PHP part:

echo $this->Form->create('Reminder', 
    array('action' => 'add',
        'inputDefaults' => array(
            'label' => false,
            'div' => false
        )
    )
); 

echo $this->Form->input('worker_id', array('empty' => 'Choose an employee'));
echo $this->Form->button('Save', array('type' => 'submit', 'class' => 'btn btn-green')); 

I did not find a way to show the prename and lastname of class Worker but keep the id as option value in the select field.

How can this be done?

  • 写回答

2条回答 默认 最新

  • dpoppu4300 2015-03-25 13:12
    关注

    You have need to create $your_listing as below

    $your_listing = array('2'=>'John Fleming','3'=>'Justin Bond');

    and used this array in your form helper as below

    echo $this->Form->input('worker_id', array('empty' => 'Choose an employee','options'=>$your_listing)); 

    So in above example, select box become

    <select name="worker_id">
      <option id="">Choose an employee</option>
      <option id="2">John Fleming</option>
      <option id="3">Justin Bond</option>
      </select>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据