How to arrange groups in ascending order in a drop down menu

Hello everyone... I want to display all the groups in a drop down in ascending order. I have successfully used the following code to get all the groups but they are not arranged in a proper order.

My Code is:

<select>
<?
$all_groups = elgg_get_entities(array("type" => "group", "limit" => ""));
foreach ($all_groups as $group){
echo '<option value="'.$group->guid.'">'.$group->name.'</option>';
?>
</select>

How to make it in Ascending order by name...???

  • $dbprefix = elgg_get_config("dbprefix");

    $group_options["joins"]    = array("JOIN " . $dbprefix . "groups_entity ge ON e.guid = ge.guid");
    $group_options["order_by"] = "ge.name ASC";

    $content = elgg_list_entities_from_metadata($group_options);

  • @happy gupta : You are not using the elgg way fo coding. do it like 

    $options_values = array();
    $groups = elgg_get_entities(array(
    'type' => 'group',
    'limit' => 0,
    'joins' => array("JOIN {$db_prefix}groups_entity g on e.guid = g.guid"),
    'order_by' => "g.name ASC",
    ));
    foreach($groups as $group){
    $options_values[$group->guid] = $group->name;
    }
    echo elgg_view('input/dropdown', array('name' => 'group_list', 'options_values' => $options_values, 'value' => $value));

    Edit : @Rvr : You were more fast :)

  • @RvR.. Your code doesn't return anything... its blank..!!!
    i used it like this...

    $dbprefix = elgg_get_config("dbprefix");
    $group_options["joins"]    = array("JOIN " . $dbprefix . "groups_entity ge ON e.guid = ge.guid");
    $group_options["order_by"] = "ge.name ASC";
    $content = elgg_list_entities_from_metadata($group_options);
    foreach ($content as $group){
    echo '<option value="'.$group->guid.'">'.$group->name.'</option>';
    }

     

    @Team Webgalli.. Your code is giving a Fatal Error.
    An unrecoverable error has occurred and has been logged. Contact the site administrator with the following information:
    Exception #1366547285.

  • Look into error logs and debug it, put some efforts from your side also.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking