Forums

[Array] - Need help to populate dropdown

Fawaaz 29 Mar, 2015
Hello,

I'm running the code below to obtain the name of all users in UserGroup 10 but I can't find a way to populate a dropdown(on form LOAD) from this result. I have read many posts from this forum but still can't..
Can you help me to populate this dropdown please?

<?php
$registeredUsers = JAccess::getUsersByGroup(10);
foreach($registeredUsers as $user_id) {
$results = array();
$user = JFactory::getUser($user_id);
$user1 =  (array) $user->name;
echo json_encode ($user1);

//Missing some code here populate dropdown
}
?>


The following is being echoed back : ["Agent 1"]["Agent 2"]


//Tried many codes like those below but none is working.
//$form->data['agent'] = json_encode($user1);
//foreach ($user1 as $t ) {
//  $form->data['agent'][] = array ( 'text' => $t, 'value' => $t);
//}
//echo json_encode($user1 [$form->data["agent"]]); //from demo-dynamic-dropdown
GreyHead 30 Mar, 2015
Hi Fawaaz,

Please try this
<?php
$registeredUsers = JAccess::getUsersByGroup(10);
$form->data['results'] = array();
foreach( $registeredUsers as $user_id ) {
  $user = JFactory::getUser($user_id);
  $form->data['results'][] = array(
    'id' => $user->id,
    'name' => $user->name
  };
}
?>
You should then be able to use 'results', 'id' and 'name' in the Dynamic Settings for the drop-down.

Bob
This topic is locked and no more replies can be posted.