php input 空,php - 如果$ this-> Form-> input为空,则不显示字段 - 堆栈内存溢出

我正在使用MongoDB数据库结构通过CakePHP2创建一个Cookbook。 我已经制作了食谱控制器,创建了视图,创建了其他功能(编辑,添加等)。现在,我可以正确添加食谱,但是当我单击旁边的编辑链接时,也可以对其进行编辑我的食谱中,所有内容均正常显示,但是如果字段为空,我不希望显示字段。

在我的数据库中,我的标准结构如下:

食谱

名称:类型:准备时间:烹饪时间:

蔬菜:肉类:其他:香料:

餐具:电器:其他:

从本质上讲,它只是echo $ this-> Form-> input字段,您可以在其中键入与数据库中该数据段相对应的值。

我要解决的问题:

显然,使用不同的食谱,您将不会在甜点中添加“肉”,或者在其他菜肴中也不会包含蔬菜等。

当我单击“编辑配方”,并且我的所有字段都弹出时,我希望隐藏空字段。

如果单击“巧克力蛋奶酥”食谱的“编辑食谱”,则其中没有肉类或蔬菜,如果数据库中没有该值,则我不希望这些输入字段显示。

这是整个edit.ctp代码:

echo $this->Form->create('Recipe');

echo '

Change The Name of The Recipe
';

echo $this->Form->input('Name');

echo '

Change The Type of Food
';

echo $this->Form->input('Type');

echo '

Change The Prep & Cook Time
';

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 '

Change The Ingredients Needed
';

echo 'Vegetables';

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 'Meats';

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 'Misc';

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 'Spices';

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 '

Change Tools Needed
';

echo 'Cutlery';

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 'Appliances';

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 'Misc';

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代码:

公共功能edit($ 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。

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值