Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在cakephp选择选项中添加默认值?_Cakephp_Cakephp 2.0 - Fatal编程技术网

如何在cakephp选择选项中添加默认值?

如何在cakephp选择选项中添加默认值?,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我想在输入字段中添加默认选择选项 例如,如果我纠正了这个代码 echo $this->Form->input('field', array( 'options' => array(1, 2, 3, 4, 5), 'empty' => '(choose one)' )); 我想像这样修改代码 echo $this->Form->input('field', array( 'options' => array(1, 2, 3, 4,

我想在输入字段中添加默认选择选项 例如,如果我纠正了这个代码

echo $this->Form->input('field', array(
    'options' => array(1, 2, 3, 4, 5),
    'empty' => '(choose one)'
));
我想像这样修改代码

echo $this->Form->input('field', array(
    'options' => array(1, 2, 3, 4, 5),
    'default' => options[1];  // it's not correct, I just want to add 2 as a default value.
));
这里我想添加选项2作为默认值。

你可以试试这个

$options = array(1, 2, 3, 4, 5);
$attributes = array('value' => 2, 'empty' => false);
echo $this->Form->select('field', $options,$attributes);
这是烹饪书上写的

如果您正在从数据库获取结果,然后填充选择选项,那么只需将值放入$this->request->data['Model']['field']='value';在控制器中,它将是“选择”下拉列表中的默认值

$options = array(1, 2, 3, 4, 5);
$attributes = array('value' => 2, 'empty' => false);
echo $this->Form->select('field', $options,$attributes);
这是烹饪书上写的


如果您正在从数据库获取结果,然后填充选择选项,那么只需将值放入$this->request->data['Model']['field']='value';在控制器中,它将是选择下拉列表中的默认值,问题在于您编写的PHP。您正在尝试引用默认值不存在且不是正确的PHP变量的内容:-

echo $this->Form->input('field', array(
    'options' => array(1, 2, 3, 4, 5),
    'default' => options[1];  // it's not correct, I just want to add 2 as a default value.
));
选项[1]不是有效的PHP变量,因为您缺少$symbol并且尚未定义$options数组。您刚刚向输入的options属性传递了一个数组

您需要先定义$options数组,然后将其传递给$this->Form->input,如下所示:-

$options = array(1, 2, 3, 4, 5);
echo $this->Form->input('field', array(
    'options' => $options,
    'default' => $options[1]; // '2' in the defined array
));

问题在于您编写的PHP。您正在尝试引用默认值不存在且不是正确的PHP变量的内容:-

echo $this->Form->input('field', array(
    'options' => array(1, 2, 3, 4, 5),
    'default' => options[1];  // it's not correct, I just want to add 2 as a default value.
));
选项[1]不是有效的PHP变量,因为您缺少$symbol并且尚未定义$options数组。您刚刚向输入的options属性传递了一个数组

您需要先定义$options数组,然后将其传递给$this->Form->input,如下所示:-

$options = array(1, 2, 3, 4, 5);
echo $this->Form->input('field', array(
    'options' => $options,
    'default' => $options[1]; // '2' in the defined array
));

如果我的输入像echo$this->Form->input'book_category_id',如果我希望第二个数据是默认值,那么您的代码中是否有可能?您是否从数据库中获取数据,并在表单中填写选项?如果我的输入类似于echo$this->form->input'book_category_id',如果我希望第二个数据是默认值,那么您的代码中是否可以?您是否从数据库中获取数据,并在表单中填写选项?这不会设置默认值。值将覆盖$this->request->data中设置的任何不同值。这不会设置默认值。值将覆盖$this->request->data中设置的任何值,该值不是同一事物。