CodeIgniter Forums

Full Version: dynamic dropdown with form generation library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Mantra of Doom[/eluser]
Hi everyone,

I have two dropdown select lists in a form. One is for series (a category) and one is for games (sub-category). I'd like to be able to populate these lists from a database table and to have the series list control what the games list shows. I know I need to use jquery or something similar to do the calling, but I'm having difficulty wrapping my head around it. I'm also using the form generation library to create my forms.

So far, I did get the lists to populate from the database tables, but I don't quite know where to go from here for the limiting of sub-categories. I've been reading tutorials, but the first issue I'm running into is that I can't seem to get "onchange=" into the select properties with the Form Generation Library. I've tried adding it both as an array and a string and it never seems to show up in my code. The second thing that I'm struggling with is trying to pull the catergores and games based on the database tables. I don't want to have to write these values into another file unless I have to, since more items may be added to the database.

Here's my code so far, if anyone has done something similar or can point me in the right direction, I'd be very grateful. I've been working on this part for a while.

Game table:

Series table:

function create(){
     $data = $this->characters_model->general();
     $user = $this->ion_auth->get_user();
// select for gender dropdown box
     $genders = array(
// Select for series and game boxes
     $game_list = $this->characters_model->get_game_dropdown();
     $series_list = $this->characters_model->get_series_dropdown();

// Start Character Creation Form        
     ->fieldset('Character Info')
     ->html('<p>Put basic Character info here</p>')
     ->hidden('player', $user->username, 'required|trim|xxs_clean') // Player Name
     ->hidden('player_id', $user->id, 'required|trim|xxs_clean') // Player ID
     ->text('name', 'Character Name', 'required|trim|xxs_clean') // Character Name
     ->select('gender', $genders, 'Gender', 'Unknown') // Gender dropdown
     ->select('category',$series_list,'Series') // Series, pulls from Series Table
     ->select('game', $game_list, 'Game') // Games, pulls from games table
     ->submit('Create Character')
     ->onsuccess('redirect', array('characters/my'));

     $data['form'] = $this->form->get();
     $data['errors'] = $this->form->errors;
     $data['websubtitle']    = 'Create Character: Basic Character Info';
     $data['content'] = 'characters/sheet';
     $this->load->view('template', $data);

    function get_game_dropdown(){
        $key = 'game';
        $value = 'game';
        $from = 'games';
        $result = array();
        $array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');
       foreach ($array_keys_values->result() as $row)
            $result[$row->$key]= $row->$value;
        return $result;
    function get_series_dropdown(){
        $key = 'series';
        $value = 'series';
        $from = 'game_series';
        $result = array();
        $array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');
       foreach ($array_keys_values->result() as $row)
            $result[$row->$key]= $row->$value;
        return $result;

El Forum

[eluser]Mantra of Doom[/eluser]
I've solved my onchange issue. Now I just need to figure out how to filter the games into the second select.