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

[eluser]Bisquite[/eluser]
Hi,

I know this question has been asked many times but I just can't implement the recommendations in my own solution. I have a form that has a dropdown box showing all the teams in the database, when the form is submitted the ID of that team is recorded in the database (the name is purely for displaying in the dropdown list).
So far I have:-

Model
Code:
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";
    return $this->db->query($query);
   }

View
Code:
<tr>
&lt;?php foreach($q_team_dropdown->result() as $row ){
      $team_dropdown[$row['team_id']] = $row['team_name'];
   }
?&gt;
   <td>Pick a team</td>
   <td>&lt;?php echo form_dropdown('form1', $team_dropdown) ?&gt;</td>
</tr>

Controller
Code:
$data['q_team_dropdown'] = $this->Matches_model->get_team_dropdown();
    $this->load->view ('matches/new_match', $data);

This all worked fine when I just had the list of names, but now I've introduced the ID as a value and I get the error
Cannot use object of type stdClass as array

Any help is much appreciated.

Thanks,
b/
#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"'
);
#3

[eluser]ivantcholakov[/eluser]
I did some editions on the previous post, this is it.
#4

[eluser]Bisquite[/eluser]
Thanks for the quick response. I am using CI 2 so have gone with your section of code that doesn't use the array_column function.

However I am now getting the errors:-
"Undefined variable: team_dropdown"
and
"Undefined variable: team_id"

Can't work out why...
#5

[eluser]ivantcholakov[/eluser]
$team_dropdown is the variable that you have to pass from the to the view.
$team_id contains the currently selected item key of the dropdown.

Code:
// Controller

    $team_dropdown = $this->Matches_model->get_team_dropdown();

    $team_id = '';  // This is an initial value.
    // If there is a database record - assign the actual value of $team_id.

    $data = array(
        'team_dropdown' => $team_dropdown,
        'team_id' => $team_id,
        // etc.
    );

    $this->load->view('my_view_with_my_form', $data);
#6

[eluser]CroNiX[/eluser]
The problem in the original post is this line:
Code:
&lt;?php foreach($q_team_dropdown->result() as $row ){

using result(), it returns an object which you can't access like an array like the error says. Change it to $q_team_dropdown->result_array() and your code should work since the result set will be an array.
#7

[eluser]Bisquite[/eluser]
Awesome thanks, now it works a treat. Of course it's an array! :cheese:




Theme © iAndrew 2016 - Forum software by © MyBB