R
R
raycheel2014-04-07 04:39:47
Yii
raycheel, 2014-04-07 04:39:47

How to properly implement select lists in Yii ?

Hello! Help me please. I have a form for adding a post, it needs to specify the author_id , I want to make me select them from the list by name, and not enter the id. How can I implement this? Please help me in detail if possible. Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Voronkov, 2014-04-07
@raycheel

<?php 
    echo $form->dropDownList(
    $model,
    'author_id',
    CHtml::listData(Authors::model()->findAll(), 'id', 'name')
    ); 
?>

R
Ruslan Kasymov, 2014-04-07
@HDAPache

dropDownList
Read more www.yiiframework.com/doc/guide/1.1/en/form.builder

B
Blah Blag, 2014-04-07
@dsadasdad

I'm building sites in Laravel and was also wondering how to implement such a list. Of course, the framework provides its own tools for this, but I did not bother and did this:

<select name="author_id">
/*В ларавеле это делалось через функцию шаблонизатора  @foreach, в yii есть своя */
     @foreach($author as $auth) 
          /* Фигурные скобки заменяют оператор echo, ну это так если че */
          <option value="{{ $auth->id }}"> {{ $auth->name }}</option>
     @endforeach
</select>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question