我创建了一个表单,它有一个循环来显示多行。我使用表单助手对每一行进行了选择。它创建的 ID 都是一样的,有没有办法向 ID 添加计数器或一些定义信息?
我正在使用 $this->Form->input('city_id')
从我的城市模型中输出选定的城市。所有 ID 均为 ModelCityId
。我想得到类似 ModelCityId1、ModelCityId2
、ModelCityId3
等的东西。这可能吗?或者是否有更好的方法来循环显示选项?
感谢您提出任何建议。
这是代码的相关部分。
while ($current_date != $departure_date) {
$current_date = date("d-M-y", strtotime($current_date . " +1 day"));
$output .= '<tr>';
$output .= '<td>'.$current_date.'</td>';
// irrelevant other columns
$output .= '<td>'.$this->Form->input('city_id', array('label' => '', 'empty' => true)).'</td>';
$output .= '</tr>';
}
最佳答案
正如 itchy 指出的那样,只需在 while
循环中使用一个计数器来获得一个唯一的数字。
然后您所要做的就是将其分配给您的 ID 字段
$this->Form->input('city_id', array('id' => 'somvalue' . $i));
假设 $i
已在您的循环中定义。
关于php - cakePHP:如何更改表单元素的 ID(使用表单助手)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163783/