【问题标题】:How use form select in cake php with foreach如何在蛋糕 php 中通过 foreach 使用表单选择
【发布时间】:2016-04-11 23:54:35
【问题描述】:

我想用表格信息填写表格选择:“foreach” 我找不到解决方案,有人可以帮助我!我使用 cakephp 2.5.5。 我想要这样的解决方案,但是使用 cake php。

  <select id="Select" name="section_id" class="form-control">
    @foreach($sections as  $section)

    <option value="{{$section->id}}">{{$section->section_name}}</option>
    @endforeach
  </select>

【问题讨论】:

  • 正确解释。你想要什么?
  • @Tony stark 我想从数据库中的表格中填写表格选择!
  • 表示要在选择框中自动填充数据库保存的数据?
  • @Tony stark 是的,我的意思是一样的,我希望你能帮助我!谢谢)

标签: cakephp-2.x


【解决方案1】:

试试这个
首先像这样从数据库中获取数据:

$result = $this->ModelName->findById('id');
$this->set('result',$result);

然后签入查看文件

<select id="Select" name="section_id" class="form-control">
@foreach($sections as  $section)

<option <?php echo ($result['ModelName']['section_id'] == $section->id) ? 'selected="selected"' : '' ?> value="{{$section->id}}">{{$section->section_name}}</option>
@endforeach
</select>

使用 Cakephp 表单助手创建选择框,让蛋糕自动显示选择的数据,像这样

$this->From->select('ModelName.select_box_name',$sections);

【讨论】:

  • 问题 cake php 不接受 select 的类型,所以我的问题是 select 的语法“与上面的选择我无法向数据库插入值”)!!
  • 我找到了解决方案: $en_atttente= $this->User->find('list', array( 'fields' => array('User.username'), 'conditions' => array('User.role' => 'respensable'), 'recursive' => 0 )); $this->set('users',$en_atttente); ,谢谢你的帮助))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多