Welcome Guest, Not a member yet? Register   Sign In
Chained Select with Form Generation Library
#1

[eluser]Mantra of Doom[/eluser]
Hello everyone. I've been struggling with creating a chained select box that pulls from two database tables. So far, I have the first select pulling from the database and the second select pulling the options from the .js file.

I'm also getting an error related to my second select box,
Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 324

When I comment out the second select option, the error goes away. Is there someone that can look over this code to find what might be causing this form_helper error? I am using 2.0 reactor with an unmodified form_helper.php.

Also, I'm stuck on how to pull the second select values from the database, as nothing I try seems to work. Getting this to work may help with the first error message as well. If anyone has any ideas, I'd love to hear them.

Thanks in advance!

Series Table: ID|Series
Games Table: ID|Game|SeriesID

The Controller code:
Code:
//---------- Character Creation Form ----------
     function create(){
    $this->load->model('characters_model');
    $data = $this->characters_model->general();
        
    // select for gender dropdown box
    $genders = array(
               'Male'=>'Male',
               'Female'=>'Female',
               'Unknown'=>'Unknown',
            );
                            
    // Values for the Series selection box
    $series_list = $this->characters_model->get_series_dropdown();
                
    // Start the Form
    $this->form
         ->fieldset('Character Info')
         ->hidden('player', $data['user']->username, 'required|trim|xxs_clean') // For creating the user's upload directory
         ->hidden('player_id', $data['user']->id, 'required|trim|xxs_clean') // Adds the userid to the character field
         ->text('name', 'Character Name', 'required|trim|xxs_clean')
         ->select('gender', $genders, 'Gender', 'Unknown')
         ->text('age', 'Age', 'trim|max_length[4]|numeric|xxs_clean')
         ->select('series|series',$series_list,'Series',' ') // Series select
         ->set_att('onchange', "populate(this)") // Adds the onchange event to the series select
         ->select('game|game', '-Select Game-', 'Game', 'required|trim|xxs_clean') // Game select. This is the line where the error seems to be coming from.
         ->textarea('description','Description','required|trim|xxs_clean')
         ->br()
         ->submit('Submit Character')
         ->model('characters_model','char_insert')
         ->onsuccess('redirect', array('player/view/'.$data['user']->username)); // When finished, redirect back to the player's profile

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

The Model to pull the series values from the database:
Code:
// SERIES DROPDOWN
    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;
    }

The .js file to control the fields:
Code:
function populate(o){
            d=document.getElementById('game');
        if(!d){return;}            
        var mitems=new Array();
        mitems['-Select Series-']=['-Select Game-'];
        mitems['Aeonverse']=['Aberrant','Adventure!','Trinity (Aeon)'];
        mitems['Big Eyes, Small Mouth']=['Big Eyes, Small Mouth, 3rd ed.'];
        mitems['Dungeons & Dragons']=['Dungeons & Dragons, v3.5','Dungeons & Dragons, v4'];
        mitems['Exalted']=['Exalted, 1st ed.','Exalted, 2nd ed.'];
        mitems['GURPS']=['GURPS Generic'];
        mitems['Mutants & Masterminds']=['Mutants & Masterminds'];
        mitems['Scion']=['Scion'];
        mitems['Star Wars RPG']=['Star Wars d20'];
        mitems['World of Darkness']=['Changeling: the Lost','Geist: the Sin Eaters','Hunter: the Vigil','Mage: the Awakening','Promethean: the Created','Vampire: the Requiem','Werewolf: the Forsaken','World of Darkness'];
        mitems['World of Darkness Classic']=['Changeling: the Dreaming','Demon: the Fallen','Ghouls: Fatal Addiction','Gypsies','Hunter: the Reckoning','Kindred of the East','Mage: the Ascension','Vampire: the Masquerade','Werewolf: the Apocalypse','World of Darkness Mortal/Other','Wraith: the Oblibion'];
            
        d.options.length=0;
        cur=mitems[o.options[o.selectedIndex].value];
        if(!cur){return;}
        d.options.length=cur.length;
        for(var i=0;i<cur.length;i++)
        {
            d.options[i].text=cur[i];
            d.options[i].value=cur[i];
        }
    }
#2

[eluser]toopay[/eluser]
The PHP error tell you that you have an non-array type in your foreach() (in your view file). If you're supply foreach argument directly from your database, make sure you had the right output on your model(or place some data validation properly on your view file, before put it on foreach statement)!
This is an example of common pitfall:
Code:
function some_sql()
{
   $query = $this->db->get('foo');
   // This will generates error when the database returned 0 result, if you didnt place some data validation properly on your view file.
   if(count($query->result_array()) > 0)
   {
      return $query->result_array();
   }
}




Theme © iAndrew 2016 - Forum software by © MyBB