An easier option might be to create the superlist method for the form helper. You can call it with much less code and it will show as many concatenated fields as you want, and even use cascading lists. It overrides the default list method in form helper.
the result is like Select Name | - Last Name | - - John | - - Mary | - Diff Last Name | - - Pete | - - Ellen http://teknoid.wordpress.com/2008/09/04/findlist-with-three-or-combined-fields/ On Mar 27, 6:24 pm, JamesF <[email protected]> wrote: > ------------------- > in your controller: (i'll assume users_controller.php) > ------------------- > > function user_list() //whatever function you are using > > { > $conditions = 'ORDER BY > User.first_name ASC'; //order our list to > make sure its alphabetical by first name > $this->set('users', > $this->User->find('all', array ('conditions'=> > $conditions ))); //send the list of users to the view as $users > } > > this should pull the list of names if your models are set up properly > > ------------- > in your view: > ------------- > <? > //app/views/users/user_list.ctp > > echo $form->create('Users', array('type' => 'post', 'action' => > 'userwhateveraction')); // use formhelper to create the form called > "Users" going to whatever action you choose > ?> > > <?php // create select box label and div wrapper > ?> > > <div class="selectbox"> > <label for="UserUserName">User Name</label> > <select name="data[User][user_name]" id="users-box"> > > <?php //build selects > > //foreach goes through array $users you set in controller > > foreach ($users as $user): > > //option value defaults to associated user id > > echo '<option value="'. $user['User']['id'] . '">' . > $user['User'] > ['first_name'] . ' ' . $user['User']['last_name'] .'</option>'; > endforeach; > > ?> > > </select> > </div> > > <?php > echo $form->end('Submit'); // close the form > ?> > > hope this helps! > in this example make sure your table is called 'users' (including > whatever option prefix you may have set up in core cfg) and your > primary key is 'id'. first name is 'first_name' and last name is > 'last_name'. > > On Mar 27, 5:37 pm, dubiousandrew <[email protected]> wrote: > > > I have a first_name field and a last_name filed in my user table. > > When a form has a 'select a user' from a drop down list the list is > > populated with user ids. I want to show first_name + last_name > > instead of user_ids. Is there a way to do this without creating > > another field in my user table called 'name'? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
