我正在使用MongoDB数据库结构通过CakePHP2创建一本Cookbook。我制作了食谱控制器,我创建了视图,我已经创建了其他功能(编辑,添加等)我现在可以正确添加食谱,但我也可以编辑它们,当我点击编辑我的食谱旁边的链接,一切正常,但我不希望字段显示为空。
在我的数据库中,我的标准结构如下:
配方
名称: 类型: 准备时间: Cooktime:
蔬菜: 肉类: 其他: 香料
刀具: 家电: 其他:
它基本上只是回显$ this-> Form->输入字段,您可以在其中键入与数据库中该数据对应的值。
我要修复的内容:
显然,使用不同的食谱,你不会有"肉类"在甜点中,或者你不会在其他菜肴中加入蔬菜等。
当我点击"编辑食谱"并且我的表单会弹出我的所有字段,我希望隐藏空字段。
如果我点击"编辑食谱"对于巧克力蛋奶酥配方,其中没有肉类或蔬菜,如果数据库中没有该值,我也不希望显示这些输入字段。
这是整个edit.ctp代码:
echo $this->Form->create('Recipe');
echo '<label><center><font size="5">Change The Name of The Recipe</font></center></label>';
echo $this->Form->input('Name');
echo '<label><center><font size="5">Change The Type of Food</font></center></label>';
echo $this->Form->input('Type');
echo '<label><center><font size="5">Change The Prep & Cook Time</font></center></label>';
echo $this->Form->input('preptime', array('value' => $this->request->data['Recipe']['Time']['preptime']));
echo $this->Form->input('cooktime', array('value' => $this->request->data['Recipe']['Time']['cooktime']));
echo '<label><center><font size="5">Change The Ingredients Needed</font></center></label>';
echo '<label>Vegetables</label>';
echo $this->Form->input('Vegetables.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Vegetables']));
echo $this->Form->input('Vegetables.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Vegetables']));
echo '<label>Meats</label>';
echo $this->Form->input('Meats.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Meats']));
echo $this->Form->input('Meats.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Meats']));
echo '<label>Misc</label>';
echo $this->Form->input('Misc.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Misc']));
echo $this->Form->input('Misc.', array('value' => $this->request->data['Recipe']['Instructions']['Ingredients']['Misc']));
echo '<label>Spices</label>';
echo $this->Form->input('Spices.', array('values' => $this->request->data['Recipe']['Instructions']['Ingredients']['Spices']));
echo $this->Form->input('Spices.', array('values' => $this->request->data['Recipe']['Instructions']['Ingredients']['Spices']));
echo '<label><center><font size="5">Change Tools Needed</font></center></label>';
echo '<label>Cutlery</label>';
echo $this->Form->input('Cutlery.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Cutlery']));
echo $this->Form->input('Cutlery.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Cutlery']));
echo '<label>Appliances</label>';
echo $this->Form->input('Appliances.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Appliances']));
echo $this->Form->input('Appliances.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Appliances']));
echo '<label>Misc</label>';
echo $this->Form->input('Misc.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Misc']));
echo $this->Form->input('Misc.', array('values' => $this->request->data['Recipe']['Instructions']['Tools Needed']['Misc']));
echo $this->Form->end('Save Recipe');
这是RecipeController代码:
公共函数编辑($ id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid Recipe'));
}
$recipes = $this->Recipe->findById($id);
if (!$recipes) {
throw new NotFoundException(__('Invalid Recipe'));
}
if ($this->request->is(array('post','put'))) {
$this->Recipe->id = $id;
if ($this->Recipe->save($this->request->data)) {
$this->Flash->success(__('Your Recipe has been updated.'));
return $this->redirect(array(
'action' => 'index'
));
}
$this->Flash->error(__('You did something WRONG!'));
}
if (!$this->request->data) {
$this->request->data = $recipes;
}
}
我需要改变一些小事情,它显然并不完美,但我现在的主要目的是使用条件来隐藏空白的字段。
代码是CakePHP2,数据库是MongoDB。
谢谢!