1
<!-- language: lang-none -->

Array
(
[0] => Array
    (
        [Doctor] => Array
            (
                [id] => 1
                [doctor_name] =>  Dr. Kazi Hasinur Rahman
            )

    )

[1] => Array
    (
        [Doctor] => Array
            (
                [id] => 2
                [doctor_name] => Dr.M. Zahid Hasan
            )

    )

[2] => Array
    (
        [Doctor] => Array
            (
                [id] => 3
                [doctor_name] => Dr.M.A.Malek
            )

    )
)

This is my array of doctors. I want to show doctor_name as dropdown and use id for query when form will be submit.

in my Doctor Model:

 public $displayField = 'doctor_name';

and my view is:

<?php echo $this->Form->input('doctor_name', array('options' => $doctors)));?>

2 Answers 2

1

When you want to use data from your model in dropdowns, a find('list') is what you're looking for. In your Controller add:

$this->set('doctorNames', $this->Doctor->find('list'));

Because you've already set the displayField in your model, it will fetch that as the labels, the id's will be the values. Just as it should be. Also, if you follow naming conventions and set the variable name as $doctorNames, you no longer need to add the options part to your View, you can simply use:

$this->Form->input('doctor_name');

And it will produce a dropdown with all the doctor names in it.

Sign up to request clarification or add additional context in comments.

Comments

1

When getting the array of doctors, use $this-> Doctor-> find ('list')

This will return a key-value array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.