Welcome Guest, Not a member yet? Register   Sign In
Auto populating Select lists from DB
#1

[eluser]alvaroeesti[/eluser]


Hello,

I have some interdependent select lists working well out of codeigniter (using therefore JQUERY, Ajax) but when I am trying to do it in CI, I m not coming on.

For a start I need the easiest one to be working, that is, the first one who just gets loaded up by querying the DB without needing jquery, but not even the first select list gets loaded.

So here we go with all the modules:

VIEW
Code:
<?php echo form_open('control_form/add_all'); ?>
       <label for="f_state">State<span class="red">*</span></label>
        <select id="f_state" name="f_state">
            <option value=""></option>
            &lt;?php
            
                foreach($result as $row)
                {
                echo '<option value="' . $row-&gt;pais_id . '">' . $row->pais_name . '</option>';
                }
                  
            ?&gt;
        </select>
        
        <label for="f_city">City<span class="red">*</span></label>
        &lt;!--this will be filled based on the tree selection above--&gt;
        <select id="f_city" name="f_city" id="f_city_label">
            <option value=""></option>
        </select>
        
        <label for="f_membername">Member Name<span class="red">*</span></label>
        &lt;input type="text" name="f_membername"/&gt;
&lt;?php echo form_close(); ?&gt;

CONTROL

Code:
public function add_all()
{

        #Validate entry form information
        $this->load->model('model_form','', TRUE);        
        $this->form_validation->set_rules('f_state', 'State', 'required');
        $this->form_validation->set_rules('f_city', 'City', 'required');
        $this->form_validation->set_rules('f_membername', 'Member Name', 'required');

        $data['city'] = $this->model_form->get_state(); //gets the available groups for the dropdown

        if ($this->form_validation->run() == FALSE)
        {
              $this->load->view('view_form_all', $data); # parece ser que vuelve a meter los mismos datos que tenia la Form
        }
        else
        {
            #Add Member to Database
            $this->model_form->add_all();
            $this->load->view('view_form_success');
        }


}

MODEL

Code:
class Model_form extends CI_Model
{
      function __construct()
    {
            // Call the Model constructor
            parent::__construct();
    }

    function get_state()
    {
        $query = $this->db->query('SELECT pais_id, pais_name FROM pais');
        return $query->result();
    }
    
    
    function add_all()
    {
     $v_state = $this->input->post('f_state');
     $v_membername = $this->input->post('f_membername');
    
     $data = array(
       'pais_id' => NULL,
       'pais_name' => $v_state
      
     );
    
     $this->db->insert('members', $data);
    }
    
    

}
#2

[eluser]Aken[/eluser]
Your view's foreach() should be:

Code:
foreach ($city as $row)

Because you've named that $data array key "city" in your controller (which I assume should be "state").
#3

[eluser]alvaroeesti[/eluser]
thank you. that would probably fix it too. what happened was that I was returning a "$result" from the model, while I was having city $data['city'] as index in my array, so replacing city by result, solved it.




Theme © iAndrew 2016 - Forum software by © MyBB