On May 24, 2008, at 12:54 PM, vishal wrote:
> ... snip..
>
> I have two models(database table):User,Message
>
> I want place a drop down in my Message view file as :
>
> <? echo $form->select('messageto', $options=array(), null, array(),
> '--
> select member--'); ?>
> I want to fills this drop down from my another table (user table)
> i.e i want to fill it dynamically from database table.
>
You can set something like this in your controller:
$users = $this->User->find('list');
$this->set(compact('users'));
Assuming that the Message model and User model has relationships, you
only need to put this in your view:
<?php echo $form->input('user_id'); ?>
To make things easier, in your User model, you should have var
$displayField = 'full_name';
So when you have $form->input('user_id') in your message view, you
will have a dropdown menu, with value="user's id", and what's shown in
the dropdown is full_name field in your users table.
> Also when i edit the message ,the drop down value should be the same
> as their id.i.e
> it select the proper value (not the first one everytime).
>
According to CookBook (http://book.cakephp.org/view/182/forms), you
can have $options['selected'].
I used this in my checkbox button, and it worked. It should also work
with a dropdown menu.
It should then more or less looks something this in your view.
<?php echo $form->input('user_id', array('selected', $users['User']
['user_id']));
Hope that helps.
> this is my controller file code for Message:
>
> **************************
>
> <?php
> class MessagesController extends AppController {
>
> var $name = 'Messages';
> var $layout='user';
>
>
> var $paginate = array(
> 'limit' => 4,
> 'order' => array(
> 'Message.messagedate' => 'asc'
> )
> );
>
>
>
>
> function index() {
> //$this->set('messages', $this->User->findAll(''));
> $data = $this->paginate('Message');
> $this->set(compact('data'));
>
> }
>
> function view($id) {
> $this->Message->id = $id;
> $this->set('Message', $this->Message->read());
>
> }
>
> function delete($id) {
> $this->Message->del($id);
> $this->flash('The message with id: '.$id.' has been deleted.',
> 'http://
> 192.168.0.60/vishal/cake_1.2.0.6311-beta/app/messages/index');
> }
>
> function add() {
>
>
>
> if (!empty($this->data)) {
> if ($this->Message->save($this->data)) {
>
>
> $this->flash('Your Message has been saved.','http://
> 192.168.0.60/vishal/cake_1.2.0.6311-beta/app/messages/index');
> }
>
> }
> }
>
>
> function edit($id = null) {
> $this->Message->id = $id;
> if (empty($this->data)) {
> $this->data = $this->Message->read();
> } else {
> if ($this->Message->save($this->data['Message'])) {
> $this->flash('Your Message has been updated.',
> array('action' =>
> 'index'));
> }
> }
> }
>
> }
> ?>
>
>
> ******************************************************
>
>
> So my friend please help me in this issue,it is very urgent for me,
> otherwise i have to put the
> query manually,but i want to fill the dropdown by cake method.
>
> Thanks in advance..!!!!
>
> Vishal
> One world Technologies,
> Ambala Cantt
> India
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---