Chris
Chris

Reputation: 361

cakephp get all dropdown options from DB

I have a drop-down and drop-down options in separate tables. That's good but now I want to be able to retrieve all the options with the drop-down code. On this page I have all of the drop-downs and items. Let's say the drop-down code is dropdown1.

I'd like to be able to do

$this->Form->input('dropdownitem_id', array('options' => $dropdown['dropdown1']));

Is there a way to do this without a helper?

Upvotes: 1

Views: 3517

Answers (4)

Chris
Chris

Reputation: 361

I created a custom helper to get the exact behavior I wanted.

Upvotes: 0

Dave
Dave

Reputation: 29131

1) why do you want to do it "without a helper"?

2) Yes, use normal PHP stuff - ie foreach() loop that echos HTML content to the page

Just look at what content the helper generates, and use PHP to mimic it.

<select name="whatever">
    <?php
    foreach($items as $item) {
        echo '<option value=" . $item['id'] . '">' . $item['name'] . '</option>';
    }
    ?>
</select>

(something like that - I wrote that quickly off the top of my head, but you should get the idea)

Upvotes: 2

Kishor Kundan
Kishor Kundan

Reputation: 3165

In the controller,

$dropdownitems = $this->OtherModel->find('list');
$this->set(compact('dropdownitems'));

In your view

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

The options for select will be populated automatically.

But I don't understand, what you meant by helper ?

Upvotes: 2

Max Doumit
Max Doumit

Reputation: 1115

You should use containable behavior.

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

Then you would query the top level element.

After you have done so.

You must run a foreach loop still as Dave said and format the option.

Let me know if you need help with the containable, they are a life saver and your friend !

Upvotes: 0

Related Questions