Re: foreach loop in select option
On Mon, Jan 21, 2013 at 11:31 AM, PlanoCoder <lance@lancebarron.com> wrote:
> I need to use foreach data in
>
> $this-Form->select('player');
>
> here is my current code
>
> <select name="data['User']['player']" id="UserPlayer">
> <?php foreach($players as $player): ?>
> <option value="<?php echo $player['Player']['id'];
> ?>">Team: <?php echo h($player['Player']['team']);
> ?> Number: <?php echo
> h($player['Player']['number']); ?></option>
> <?php endforeach; ?>
> </select>
>
You'll see in the API that if you pass an associative array to
FormHelper::select() it will use the keys as the option value and the
values (of the array) as the option content.
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
So what you need to do is format a new array from the data in
$players. Right now you have:
array(
array(
'Player' => array(
'id' => 'xxx',
'team' => 'something',
'number' => 'yyy'
),
etc.
You need to change that to:
$options = array(
'xxx' => 'Team: something Number: yyy',
'xxx' => 'Team: something Number: yyy',
etc.
);
You can do this with the Set or Hash utility classes, depending what
version of Cake you are using.
Then:
echo $this->Form->select('User.Player', $options);
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
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.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home