dswg47377 2015-03-25 04: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部