how do you create a select with both value and the label the same from an array in cakephp?

一世执手 提交于 2019-12-07 07:25:13

问题


I created an array in the controller from a table :

$tasktemplateResults = $this->Tasktemplate->find('list');

And in the view I have a form helper that creates a drop down list:

echo $this->Form->input('Tasktemplate.id', array('options' => array($tasktemplateResults),'label' => 'Task'));

The html output is as follows:

<div class="input select">
    <label for="TasktemplateId">Task</label>
    <select name="data[Tasktemplate][id]" id="TasktemplateId"> 
        <option value="1">Test task</option> 
        <option value="2">Second test task</option> 
    </select>
</div>

I would like to have this html output:

<div class="input select">
    <label for="TasktemplateId">Task</label>
    <select name="data[Tasktemplate][id]" id="TasktemplateId"> 
        <option value="Test task">Test task</option> 
        <option value="Second test task">Second test task</option> 
    </select>
</div>

How do I get the name and value pairs the same as the array as shown above?


回答1:


You can add parameters to the find 'list' method to specify which fields to use for the key and the value.

Example:

    $usernameMap = $this->Article->User->
find('list', array('fields' => array('User.username', 'User.first_name')));

http://book.cakephp.org/view/1022/find-list




回答2:


array in the controller

$tasktemplateResults = $this->Tasktemplate->find('list', array('fields' => array('Tasktemplate.name')));

and in view

echo $this->Form->select('task', $tasktemplateResults);


来源:https://stackoverflow.com/questions/6531112/how-do-you-create-a-select-with-both-value-and-the-label-the-same-from-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!