Forums

How to make a dynamic dropdown list of users by name

yahhr857 19 Nov, 2010
In one of the forms used regularly by admins here, we needed a way to dynamically populate a dropdown field with usernames (employees) instead of their ID numbers, and still have it save in the new table with their ID number. I shared this in another post, but it is kind of buried under a different topic, so here it is by itself complete with the div tags.

<div class="form_item">
  <div class="form_element cf_dropdown">
   <label class="cf_label" style="width: 150px;">Select Person</label>
    <select class="cf_inputbox validate-selection" id="cf_user_id" size="1" title=""  name="cf_user_id">
    <option value="">Choose Employee</option>
    <?php
    $db =& JFactory::getDBO();
    $query = "
      SELECT *
        FROM #__users ";
    $db->setQuery($query);
    $rows = $db->loadObjectList();

foreach ($rows as $row) {
      echo "<option value='$row->id'>$row->name</option>";
}
    ?>
    </select>
    </div>
</div>
This topic is locked and no more replies can be posted.