Re: Joining two arrays to use in a Select box with the formhelper

2009-08-30 Thread FrederickD

Not one to give up easily, if at all... I gave it another try. I
remember reading a post somewhere that an easy way to add an entry to
the beginning of a dropdown list was like this:

echo $form->input(
   'county_id',
   array(
  'options' => array('Please pick a country') + $counties
   )
);

So I experimented in the ajax_dropdown.ctp file and did this:


$v) : ?>



Now it works! I can tell the ajax observeField is firing the
ajax_dropdown.ctp because I get "Pick me!" as the first entry and all
the elements in the dropdown are correct in name and in id value.

Whew!

Is there a better way to do this, or just be glad it does and move on?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-30 Thread FrederickD

Thanks Brian! I received both your message on this thread and on the
other one.

Adding the statements below in the controller function had no effect.

function getCounties() {
   $this->set('options',
  $this->Example->County->find('list',
 array(
'conditions' => array(
   'County.state_id' => $this->data['Example']['state_id']
),
'group' => array('County.name')
 )
  )
   );
   array_unshift($counties, array("none"=>"Pick me please!"));
   $this->set(compact('counties'));
   $this->render('/examples/ajax_dropdown');
}

I even tried $options instead of $counties and still no effect.

Then I got to thinking about the /examples/ajax_dropdown .ctp file. It
loops through $options and creates the entries for the dropdown. So I
modified it to look like this:

"Pick me!")); ?>
$v) : ?>



Then the first entry was the word 'array'. So I read the www.php.net/manual
for array_unshift and saw an example and tried the following:


$v) : ?>



Now I do get the value of "Pick me!" in the county dropdown after the
state is selected. So the observeField is firing. The counties are in
name order on the dropdown. However, the array_unshift has resequenced
the county_id value associated with a county name.

Apparently that is what array_unshift does. "All numerical array keys
will be modified to start counting from zero while literal keys won't
be touched."

Hopefully I have not done something wrong in my code. Perhaps because
of ajax I am not accessing the $options or $counties array in the
correct spot.

Any good ideas? The sample data and Cake file are available here:
http://forum.phpsitesolutions.com/php-frameworks/cakephp/ajax-cakephp-dynamically-populate-html-select-dropdown-box-t29.html

Thanks!

On Aug 30, 11:01 am, brian  wrote:
> You have to set() the variable afterwards. You're adding a value to a
> purely local variable.
>
> array_unshift($counties, array("none"=>"Pick me!"));
> $this->set(compact('counties'));
> $this->render('/examples/ajax_dropdown');
>


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-30 Thread brian

You have to set() the variable afterwards. You're adding a value to a
purely local variable.

array_unshift($counties, array("none"=>"Pick me!"));
$this->set(compact('counties'));
$this->render('/examples/ajax_dropdown');

On Sat, Aug 29, 2009 at 11:28 PM,
FrederickD wrote:
>
> Hey there... this thread may hold the answer to my post that is here:
>
> http://groups.google.com/group/cake-php/browse_thread/thread/25fa39d4e10bff7c#
>
> Would one of you please take a quick look at my question on the
> thread? I tried the array_unshift and put it in my controller just
> before the render, as Brian suggested, but I did not get the 'Pick
> me!' in the list after I selected a state.
>
> array_unshift($counties, array("none"=>"Pick me!"));
> $this->render('/examples/ajax_dropdown');
>
> Thoughts? Comments? Code snippet? Thank you!
>
> On Aug 27, 2:44 pm, channel5  wrote:
>> Thanks all for your help, worked a charm!
>>
>> Thanks again :)
>> c5
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-29 Thread FrederickD

Hey there... this thread may hold the answer to my post that is here:

http://groups.google.com/group/cake-php/browse_thread/thread/25fa39d4e10bff7c#

Would one of you please take a quick look at my question on the
thread? I tried the array_unshift and put it in my controller just
before the render, as Brian suggested, but I did not get the 'Pick
me!' in the list after I selected a state.

array_unshift($counties, array("none"=>"Pick me!"));
$this->render('/examples/ajax_dropdown');

Thoughts? Comments? Code snippet? Thank you!

On Aug 27, 2:44 pm, channel5  wrote:
> Thanks all for your help, worked a charm!
>
> Thanks again :)
> c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Joining two arrays to use in a Select box with the formhelper

2009-08-29 Thread channel5

Hi

I have a select box that is populated by a database:

$isps = $this->Consultation->Isp->find('list');

and then output using the form helper in the view:

echo $form->select('isp_id', $isps, array('selected' => 
$ispselect),
array(), 'select your supplier');

This works fine, however what I want to do is have an extra option
inserted between the empty element 'select your supplier' and the
first item from the array $isp , this option would allow me to have
the option selected be "none of the below".

I can't for the life of me figure out how to do this, can anyone give
me any pointers?

Thanks very much
c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-27 Thread channel5

Thanks all for your help, worked a charm!

Thanks again :)
c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-27 Thread DigitalDude

Hey,

nice one, didn't know about that. Would it be correct for an added
value at the END of the array to use array_push() ?



On 27 Aug., 20:48, Melgior  wrote:
> Hi,
>
> That's easy. You can use array_unshift to add an entry to the start of
> an array. Just add this to your controller:
>
>                 $isps = $this->Consultation->Isp->find('list');
>                 array_unshift($isps, array("none"=>"None of the above"));
>
> And you should be all set.
>
> Greets,
>
> Melgior.
>
> On 27 aug, 19:52, channel5  wrote:
>
> > Hi
>
> > I have a select box that is populated by a database:
>
> >                 $isps = $this->Consultation->Isp->find('list');
>
> > and then output using the form helper in the view:
>
> >                 echo $form->select('isp_id', $isps, array('selected' => 
> > $ispselect),
> > array(), 'select your supplier');
>
> > This works fine, however what I want to do is have an extra option
> > inserted between the empty element 'select your supplier' and the
> > first item from the array $isp , this option would allow me to have
> > the option selected be "none of the below".
>
> > I can't for the life of me figure out how to do this, can anyone give
> > me any pointers?
>
> > Thanks very much
> > c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-27 Thread DigitalDude

forgot something...

If you save the value "none" to your database and in the other cases
0, 1, 2, 3 (id's of the suppliers...) and so on your code should
probably look like this:

$form->input(Modelname.isp_id, array(
  'empty' => __('Select your supplier', true)
);

The 'empty' key of the array is used to display the first item on a
select as the defined text, set as a gettext (so you can make it
multilanguage). The Select will be builded up by cake as a select,
because your controller builded it up as a "list", so you do not need
to type $form->select.

other options of the options-array could be:
'type' => select/radio/cechbox,
'label' => set a label for the input,
and so on...

If you follow cake conventions Cake will automatically display a
selected value when editing a record.

With the hint from Melgior your list should be complete in the matter
you wanted it to look like...


On 27 Aug., 20:52, DigitalDude  wrote:
> Hey,
>
> nice one, didn't know about that. Would it be correct for an added
> value at the END of the array to use array_push() ?
>
> On 27 Aug., 20:48, Melgior  wrote:
>
> > Hi,
>
> > That's easy. You can use array_unshift to add an entry to the start of
> > an array. Just add this to your controller:
>
> >                 $isps = $this->Consultation->Isp->find('list');
> >                 array_unshift($isps, array("none"=>"None of the above"));
>
> > And you should be all set.
>
> > Greets,
>
> > Melgior.
>
> > On 27 aug, 19:52, channel5  wrote:
>
> > > Hi
>
> > > I have a select box that is populated by a database:
>
> > >                 $isps = $this->Consultation->Isp->find('list');
>
> > > and then output using the form helper in the view:
>
> > >                 echo $form->select('isp_id', $isps, array('selected' => 
> > > $ispselect),
> > > array(), 'select your supplier');
>
> > > This works fine, however what I want to do is have an extra option
> > > inserted between the empty element 'select your supplier' and the
> > > first item from the array $isp , this option would allow me to have
> > > the option selected be "none of the below".
>
> > > I can't for the life of me figure out how to do this, can anyone give
> > > me any pointers?
>
> > > Thanks very much
> > > c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-27 Thread Melgior

Yes, you could use array_push() indeed, although you don't even need
to. The PHP manual recommends this method if you want to add just one
entry:

$isps['none'] = "None of the above";

This way 'none' will show up as last in the dropdown, as long as you
put the line after ->find().

On 27 aug, 20:52, DigitalDude  wrote:
> Hey,
>
> nice one, didn't know about that. Would it be correct for an added
> value at the END of the array to use array_push() ?
>
> On 27 Aug., 20:48, Melgior  wrote:
>
>
>
> > Hi,
>
> > That's easy. You can use array_unshift to add an entry to the start of
> > an array. Just add this to your controller:
>
> >                 $isps = $this->Consultation->Isp->find('list');
> >                 array_unshift($isps, array("none"=>"None of the above"));
>
> > And you should be all set.
>
> > Greets,
>
> > Melgior.
>
> > On 27 aug, 19:52, channel5  wrote:
>
> > > Hi
>
> > > I have a select box that is populated by a database:
>
> > >                 $isps = $this->Consultation->Isp->find('list');
>
> > > and then output using the form helper in the view:
>
> > >                 echo $form->select('isp_id', $isps, array('selected' => 
> > > $ispselect),
> > > array(), 'select your supplier');
>
> > > This works fine, however what I want to do is have an extra option
> > > inserted between the empty element 'select your supplier' and the
> > > first item from the array $isp , this option would allow me to have
> > > the option selected be "none of the below".
>
> > > I can't for the life of me figure out how to do this, can anyone give
> > > me any pointers?
>
> > > Thanks very much
> > > c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Joining two arrays to use in a Select box with the formhelper

2009-08-27 Thread Melgior

Hi,

That's easy. You can use array_unshift to add an entry to the start of
an array. Just add this to your controller:

$isps = $this->Consultation->Isp->find('list');
array_unshift($isps, array("none"=>"None of the above"));

And you should be all set.

Greets,

Melgior.

On 27 aug, 19:52, channel5  wrote:
> Hi
>
> I have a select box that is populated by a database:
>
>                 $isps = $this->Consultation->Isp->find('list');
>
> and then output using the form helper in the view:
>
>                 echo $form->select('isp_id', $isps, array('selected' => 
> $ispselect),
> array(), 'select your supplier');
>
> This works fine, however what I want to do is have an extra option
> inserted between the empty element 'select your supplier' and the
> first item from the array $isp , this option would allow me to have
> the option selected be "none of the below".
>
> I can't for the life of me figure out how to do this, can anyone give
> me any pointers?
>
> Thanks very much
> c5
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---