Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/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_Timezone_Cakephp 2.0_Cakephp 2.1 - Fatal编程技术网

在选择框cakephp中显示默认值

在选择框cakephp中显示默认值,cakephp,timezone,cakephp-2.0,cakephp-2.1,Cakephp,Timezone,Cakephp 2.0,Cakephp 2.1,我是cakephp新手。。我在我的webapp中实现了一个时区功能。我使用这个时区助手类在我的选择框中显示时间 在我看来,我在像这样的选择框中回显时区 echo $this->Timezone->select('timezone'); $list = $this->Form->input($fieldname, array("type"=>"select",'default'=>'$oldtimezone', "label"=>$labe

我是cakephp新手。。我在我的webapp中实现了一个时区功能。我使用这个时区助手类在我的选择框中显示时间

在我看来,我在像这样的选择框中回显时区

  echo $this->Timezone->select('timezone');
   $list = $this->Form->input($fieldname,  array("type"=>"select",'default'=>'$oldtimezone', "label"=>$label, "options"=>$this->timezones, "error"=>"Please choose a timezone")); 
    return $this->output($list);
  echo $this->Timezone->select('timezone',array('default'=>'oldtimezone'));
我现在正在做的是,我获取用户选择的任何时间的值,然后更新数据库中的时区字段。。。我现在想要的是当用户想要再次更改时区时,如何在selectbox中将其旧时区视为默认值。。首先,我不知道如何将默认值添加到selectbox

我的助手类有这个函数

 function select($fieldname, $label="Please Choose a timezone") { 

     $list = $this->Form->input($fieldname, array("type"=>"select", "label"=>$label, "options"=>$this->timezones, "error"=>"Please choose a timezone")); 
    return $this->output($list); 
  } 
第二件事是,如果我想显示默认值,显然我必须从数据库中查询,然后检索用户的旧时区。。所以问题是如果我必须将默认值附加到我的helper类,例如

  echo $this->Timezone->select('timezone');
   $list = $this->Form->input($fieldname,  array("type"=>"select",'default'=>'$oldtimezone', "label"=>$label, "options"=>$this->timezones, "error"=>"Please choose a timezone")); 
    return $this->output($list);
  echo $this->Timezone->select('timezone',array('default'=>'oldtimezone'));
所以为了做到这一点,我应该在helper类中装入model,然后在helper类中查询吗?可能吗?还是我没有违反cakephp或mvc规则?因此,我的下一个问题是如何在helper中加载模态

因为如果我能像这样在这里添加默认值

  echo $this->Timezone->select('timezone');
   $list = $this->Form->input($fieldname,  array("type"=>"select",'default'=>'$oldtimezone', "label"=>$label, "options"=>$this->timezones, "error"=>"Please choose a timezone")); 
    return $this->output($list);
  echo $this->Timezone->select('timezone',array('default'=>'oldtimezone'));

然后,我认为无需在helper类中进行更改,因为我只需将变量从控制器传递到此视图

将默认值传递到视图的正确方法是在控制器的else块中执行此操作:

if (posted) {
    //validate and save
} else {
    //default values here
    $this->request->data['Modelname']['timezone'] = $timeZoneFromDb;
}
通过这种方式,您可以利用控制器逻辑,而不必在视图/帮助器中执行任何操作


有关详细信息,请参阅(默认值部分)。

谢谢标记。。它工作得很好。。。谢谢你,我不知道我必须为这个写一行代码