phpyiihtml.dropdownlistforyii-chtml

List to DropDown list in Yii


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!


Solution

  • 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); 
    ?>