Sunday, August 30, 2009

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

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:

<?php array_unshift($options, array("none"=>"Pick me!")); ?>
<?php foreach($options AS $k=>$v) : ?>
<option value="<?php echo $k; ?>"><?php echo $v; ?></option>
<?php endforeach; ?>

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:

<?php array_unshift($options, "Pick me!"); ?>
<?php foreach($options AS $k=>$v) : ?>
<option value="<?php echo $k; ?>"><?php echo $v; ?></option>
<?php endforeach; ?>

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 <bally.z...@gmail.com> 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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: