square_eyes
square_eyes

Reputation: 1281

Using CodeIgniter to populate drop down lists in forms

Being new to CI and PHP, especially arrays, I'm struggling with documentation and tutorials. Please don't point me to the documentation, I have been reading it all day.

As I understand it, I need a view (form), controller and model. The model connects to my default DB connection via a class/function (my DB is where my drop-down list country data is stored), it returns an array, and finally the model parses the array to the form.

Where I struggle is how to parse the array along to the view. Because I'm dealing with 3 files, to output one html file, context is really important, as is syntax. I asked this earlier today and nearly lost my mind as the two answers I got were fragmented and out of context. They were helpful but assumed I knew a lot more than I do.

In other words, please be explicit, because a syntax oversight or knowledge assumption could have me scratching my head for hours. Thanks in advance!

Here's my attempt which isn't working, basically whatever variable I use in the view returns a variable undefined error...

View/Form...

<body>   
<?php echo form_open('form'); ?> 

<h5>Country</h5>
<?php echo form_dropdown('', $data, '');?>

<div><input type="submit" value="Submit" /></div>    
<?php echo form_close() ?>    
</body>

My controller...

<?php

class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');        




        $this->load->model('country');
        $data['country'] = $this->country->get_country();
        $this->load->view('myform', $data);


        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->database();

            $sql = "INSERT INTO donations (country) VALUES (
            ".$this->db->escape($this->input->post('country')).";

            $this->db->query($sql);

            echo $this->db->affected_rows(); 

            $this->load->view('formsuccess');
        }
    }
}
?>

My model...

<?php

class Country extends CI_Model
{

function get_country() {
    $this->load->database();
    $sql = ('select * from countries');
    return $this->db->query($sql)->result();
    }
}
?>

Upvotes: 0

Views: 7284

Answers (1)

Jens A. Koch
Jens A. Koch

Reputation: 41796

Controller

You have $data['country'] with data from database, but your are not adding the $data to the view. Use $this->load->view('myform', $data); to pass the data to the view.

View

I believe codeigniter extracts the key in the $data: so simply test in the view what's there var_dump($data); and var_dump($country) and then build the form_dropdown.

Use echo form_dropdown('country_dropdown', $country, ''); in your view.

Model

Adjust the model to return the array structure needed for form_dropdown.

function get_country() {
  $this->load->database();
  $sql = ('select * from countries');
  $query = $this->db->query($sql);

  foreach($query->result_array() as $row) {
    $data[$row['id']] = $row['name'];
  }

  return $data;
}   

Upvotes: 1

Related Questions