简体   繁体   中英

Codeigniter dropdown doesn't display the selected value from database

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);
    }
}?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM