Welcome Guest, Not a member yet? Register   Sign In
Using form_dropdown with IDs and names
#2

[eluser]ivantcholakov[/eluser]
This is written for CodeIgniter 3 in mind, it may work in CodeIgniter 2 (or minor adaptation would be needed):

Code:
// Model

public function get_team_dropdown() {

    $query = "
    select team_id, team_name
    from teams
    where club_id = 1
    union
    select team_id, team_name
    from
    (
         select team_id, team_name
         from teams
         where club_id <> 1
         order by team_name
    ) as s";

    $result = $this->db->query($query)->result_array();

    // The function array_column() always exists in CodeIgniter 3.
    // See http://www.php.net/manual/en/function.array-column.php
    $result = array_column($result, 'team_name', 'team_id');

    // Or if don't have array_column():
    //$options = array();
    //foreach ($result as $row)
    //{
    //    $options[$row['team_id']] = $row['team_name'];
    //}
    //$result = $options;

    return $result;
}

Code:
// Controller

$team_dropdown = $this->Matches_model->get_team_dropdown();
// Pass this variable to the view.

Code:
// View

echo form_dropdown(
    'team_id',
    array('' => '-- Choose --') + $team_dropdown,
    set_value('team_id', $team_id),
    'id="team_id"'
);


Messages In This Thread
Using form_dropdown with IDs and names - by El Forum - 04-10-2014, 11:39 AM
Using form_dropdown with IDs and names - by El Forum - 04-10-2014, 12:23 PM
Using form_dropdown with IDs and names - by El Forum - 04-10-2014, 12:41 PM
Using form_dropdown with IDs and names - by El Forum - 04-10-2014, 02:55 PM
Using form_dropdown with IDs and names - by El Forum - 04-10-2014, 03:08 PM
Using form_dropdown with IDs and names - by El Forum - 04-10-2014, 04:58 PM
Using form_dropdown with IDs and names - by El Forum - 04-13-2014, 11:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB