Sunday, January 30, 2011

Re: Numeric Keys with Form Select

hi,
ur issue is very often,

now use
echo $this->Form-
>input('category_id',array('id'=>'SearchCategories') );
//assuming that ur has category_id as first field

but if u have fields id and then category_id then in ur controller u
need to fetch the data by following way

$this->Categories->find('list', array('Category.category_id',
'Category.name'));
$this->set(compact('categories'));

and i ur view u nedd to use it like bellow

$this->Form->input('Category', array('options'=>$category));

i hope this will display u the required output,
reply if it works

best regards

Andy


On Jan 31, 10:55 am, Jeremy Burns | Class Outfit
<jeremybu...@classoutfit.com> wrote:
> By default, the helper will use the id and name (or title) fields from your related lookup table, and you populate the options by:
>
> $categories = $this->[Model]->Category->find('list'); ([Model] is the model associated with the current controller)
>
> You then pass that variable (using set) into the view from your controller. The input:
>
> echo $this->Form->input('category_id');
>
> ...will then automatically look for a variable called $categories. If it is present, it will use those values, or the variable you have specified ('category_list', in your case).
>
> If you are deviating away from this standard you can expect the unexpected and the need to do some hacking. Looks like the problem is the way you are building your options array, or possibly the data type of the underlying field, 'category'; is that a varchar/char? It really ought to be called category_id to meet with conventions, and I would use Form->input, not Form->select.
>
> Jeremy Burns
> Class Outfit
>
> jeremybu...@classoutfit.comhttp://www.classoutfit.com
>
> On 29 Jan 2011, at 21:05, richfarr wrote:
>
> > 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 &amp; Art</option>
> > <option value="2">Baby/Children Needs</option>
> > <option value="3">Boats/Marine/Fishing</option>
> > <option value="4">Books &amp; Magazines</option>
> > ...
>
> > // I'd like it to output like this
> > <option value="164">Antiques &amp; Art</option>
> > <option value="83">Baby/Children Needs</option>
> > <option value="176">Boats/Marine/Fishing</option>
> > <option value="222">Books &amp; Magazines</option>
> > ...
>
> > --
> > Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help others with their CakePHP related questions.
>
> > To unsubscribe from this group, send email to
> > cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: