0

I am new to codeigniter and created a dropdown with the fields of my database. So, when I select particular field and click the submit button it should display all those values from my database. I am getting confused on how to pass the field value to my controller file and then to my model file. Can we pass the values only as a array to model or view file?

Controller file:

class Dropcontroller extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

        public function index()
        {
            $this->load->helper('form');
            $this->load->view('dropview');

        }

        public function data_submitted(){
            $this->load->helper('array');
            $data = array(
            'name'=>$this->input->post('details')
            );
            $this->load->model('Dropmodel');
            $this->Dropmodel->select($data);
            $this->load->model('Dropmodel');
            $result = $this->Dropmodel->select($data);
            $data['result'] = $result[0];
            $this->load->view('dropview',$data);
        }

}

Model file:

"My database name is USERS and contains three fields NAME, EMAIL, ID.When i select NAME from dropdown all the names in my database should be displayed"

class Dropmodel extends CI_Model{
    function __construct(){
        parent::__construct();  //call the model constructor
        }

    function select($data){
        $query = $this->db->query('SELECT $data from users');
        if ($query->num_rows()>0) {
            $data = $query->result();
        return $data;
        } else {
        return false;
        }
    }

view file:

?php echo form_open('Dropcontroller');?>    
<?php echo '<h1>Creating a dropdown and displaying the results from database</h1>';?><br/>
<?php $options = array(
        'name'         => 'Name',
        'email'           => 'Email',
        'id'         => 'Id',

);

echo form_dropdown('details',$options);?><br/></br>
<?php echo form_submit(array('id'=>'submit','value'=>'submit'));?><br>



<?php echo form_close();?>
<?php if(isset($result)){
    foreach($result as $item){
        print_r($item);
    }
}?>

1 Answer 1

0

you are not passing correct action in your form. You are just using controller name that will not work.

you need to

<?php echo form_open('Dropcontroller/data_submitted');?>.

if you are submitting data to data_submitted.

Let me know if you have any confusion.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked azam. but i don't know how to pass the select field value in to the query of my model page. I am storing it in array called data but when i include this in my query it displays data error. Please loot at my model page code above and suggest me a solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.