1

Please help with this code:

<ul class="activity-projects">
    <?php foreach ($ownProjects as $userProject) : ?>
    <li>
        <a class="<?php echo ($userProject->id == $project_id) ? 'active' : '';?>"
            href="<?php echo Yii::app()->createUrl('user/activity', array(
                'user_id'=>$user->id,
                'project_id'=>$userProject->id,
                'date'=>$date)
            ); ?>"><?php echo $userProject->name; ?></a>
    </li>
    <?php endforeach; ?>
    <?php foreach ($projects as $userProject) : ?>
    <li>
        <a class="<?php echo ($userProject->project_id == $project_id) ? 'active' : '';?>"
            href="<?php echo Yii::app()->createUrl('user/activity', array(
                'user_id'=>$user->id,
                'project_id'=>$userProject->project->id,
                'date'=>$date)
            ); ?>"><?php echo $userProject->project->name; ?></a>
    </li>
    <?php endforeach; ?>
</ul>

How to change it to dropdown list, using CHtml::dropDownList. Thanks for watching!

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
k1r8r0wn
  • 780
  • 9
  • 21

3 Answers3

1

First, you need to define a key-value array like this:

$options = array();
<?php foreach ($ownProjects as $userProject) 
    array_push($options, array($userProject->id => $userProject->name));
?>

 echo CHtml::dropDownList('seletName', '1', $options);

This will be produced an html <select> tag with "seletcName" name. And also option with value "1" will be selected option. You can use your desired value for first and second parameters. Also you can use CActiveForm#dropDownList for this purpose.

hamed
  • 7,939
  • 15
  • 60
  • 114
1

In your form, use the form dropDownList() function.

<?php echo $form->dropDownList(
               $model,
               'project_id',
               CHtml::listData(OwnProjects::model()->findAll(),
                   'id', 'name'),
               array('empty' => '(Select project)','class'=>"form-control")
          );
?>

From your example, it looks like OwnProjects is not a model on its own, but a subset of a model. You can customise the query

<?php echo $form->dropDownList(
               $model,
               'project_id',
               CHtml::listData(OwnProjects::model()->findAllByAttributes(array('user_id'=> Yii:app()->user->id),
                   'id', 'name'),
               array('empty' => '(Select project)','class'=>"form-control")
          );
?>
crafter
  • 6,246
  • 1
  • 34
  • 46
0

This solution finally helps me:

<?php  $options = array();
  foreach ($projects as $userProject) :
    $options[$userProject->id] = $userProject->project->name;
  endforeach;

  echo CHtml::dropDownList('selectName', '1', $options); 
?>
k1r8r0wn
  • 780
  • 9
  • 21