在 CakePhp FormHelper Select 中使用数字键
我正在使用 FormHelper (cake 1.3) 从数组创建一个选择框。该数组使用数字作为键,但选择框会忽略这些数字,并为选择框选项值使用从零开始的索引。我尝试使用 (string)$key 和 strval($key) 将数组键键入字符串,但没有成功。当我在数字键之前添加一个字母(即 'c'.$key )时,选择选项起作用,但我想避免这种黑客行为。
有没有办法强制 FormHelper 使用实际的数字键而不在前面添加字母?任何帮助将不胜感激。
请参阅以下代码进行说明:
// $category_list looks like this
Array
(
[164] => Antiques & Art
[83] => Baby/Children Needs
[176] => Boats/Marine/Fishing
[222] => Books & Magazines
[287] => Building Materials
[215] => Business
[175] => Caravans & Motor Homes
[169] => Cars & Other Vehicles
[127] => Clothing & Accessories
[92] => Computers & Electronics
[358] => Farm & Agriculture
[235] => Garage Sales/Yard Sales
[309] => Garden & Yard
[178] => General Merchandise
[138] => Health & Beauty
[186] => Hobbies & Collectables
[63] => Household
[234] => Information
[388] => Motorbikes & Scooters
[206] => Musical Instruments
[449] => Notices
[305] => Pets and Accessories
[242] => Positions Vacant
[236] => Real Estate & Rentals
[243] => Services
[143] => Sports Equipment
[308] => Tools & Equipment
[300] => Travel & Holiday
)
// Output category select box
echo $form->select(
'category',
$category_list,
$category,
array('id'=>'SearchCategories')
);
// Outputs like this
<option value="1">Antiques & Art</option>
<option value="2">Baby/Children Needs</option>
<option value="3">Boats/Marine/Fishing</option>
<option value="4">Books & Magazines</option>
...
// I'd like it to output like this
<option value="164">Antiques & Art</option>
<option value="83">Baby/Children Needs</option>
<option value="176">Boats/Marine/Fishing</option>
<option value="222">Books & Magazines</option>
...
I'm using the FormHelper (cake 1.3) to create a select box from an array. The array uses numbers as keys but the select box ignores those numbers and uses a zero-based index for the select box option values. I've tried typing the array key to a string using both (string)$key and strval($key) with no luck. The select options work when I prepend a letter before the numeric key (i.e. 'c'.$key ) but I'd like to avoid this hack.
Is there a way to force FormHelper to use the actual numeric keys without prepending a letter? Any help would be appreciated.
See the following code for illustration:
// $category_list looks like this
Array
(
[164] => Antiques & Art
[83] => Baby/Children Needs
[176] => Boats/Marine/Fishing
[222] => Books & Magazines
[287] => Building Materials
[215] => Business
[175] => Caravans & Motor Homes
[169] => Cars & Other Vehicles
[127] => Clothing & Accessories
[92] => Computers & Electronics
[358] => Farm & Agriculture
[235] => Garage Sales/Yard Sales
[309] => Garden & Yard
[178] => General Merchandise
[138] => Health & Beauty
[186] => Hobbies & Collectables
[63] => Household
[234] => Information
[388] => Motorbikes & Scooters
[206] => Musical Instruments
[449] => Notices
[305] => Pets and Accessories
[242] => Positions Vacant
[236] => Real Estate & Rentals
[243] => Services
[143] => Sports Equipment
[308] => Tools & Equipment
[300] => Travel & Holiday
)
// Output category select box
echo $form->select(
'category',
$category_list,
$category,
array('id'=>'SearchCategories')
);
// Outputs like this
<option value="1">Antiques & Art</option>
<option value="2">Baby/Children Needs</option>
<option value="3">Boats/Marine/Fishing</option>
<option value="4">Books & Magazines</option>
...
// I'd like it to output like this
<option value="164">Antiques & Art</option>
<option value="83">Baby/Children Needs</option>
<option value="176">Boats/Marine/Fishing</option>
<option value="222">Books & Magazines</option>
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您坚持惯例,您应该能够执行 $this->Form->input('category_id') 并且它将正常工作(将 $categories 传递给视图)。 Cake 确实使用数组的键作为选择中的值,因此您必须传递错误的数据。
我猜您已经对数组执行了 sort() 操作,这将重置所有键。查看 php 手册,了解按值排序并保持键完整的方法,asort() iirc
if you stuck to conventions you should be able to do $this->Form->input('category_id') and it will just work (with passing $categories to the view). Cake does use the key of the array for the value in the select so you must be passing the wrong data.
I am guessing you have done sort() on the array which will reset all the keys. Look at the php manual for the method that sorts by value keeping keys intact, asort() iirc