简体   繁体   中英

CodeIgniter get values from database in select box?

I am new to CodeIgniter, so I dont know how to do this. I want to display values dynamically in a select box and after selecting the value it displays a textbox and then it then pass the textbox value and and the option( the names which is displayed on dropdown list) id to controller,so briefly what I want to do:

  • dynamically show the values in select box
  • after selecting the value dynamically create textBox
  • passing the selected or track the 'id' of dropdown list and textbox value to controller

here is my Model

function getAllCategories(){
    $this->db->select('cat_name');
    $q = $this->db->get('category');

    if ($q->num_rows() > 0){
        foreach($q->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }

}

my controller

function showCategoryNames(){
    $data = array();
    $this->load->model('categoryModel');
    $query = $this->categoryModel->getAllCategories();
    if ($query){
        $data['records'] = $query;  
    }    
    $this->load->view('itemsView',$data);   
 }

View: this is showing the simple list

<?php if(isset($records)) : foreach($records as $row) :?>
    <h2><?php echo $row->cat_name; ?></h2>
    <?php endforeach;?>
    <?php else :
endif;?>

how about

<select name="mySelect">
<?php foreach($records as $row) { ?>
<option value="<?=$row->id?>"><?=$row->cat_name?></option>
<?php } ?>
</select>

in your view?

Here is a tutorial about working with jQuery, Ajax and Codeigniter:

http://www.jotorres.com/2012/01/using-jquery-and-ajax-with-codeigniter/

在加载表单助手类之后,您的视图应该用于创建下拉列表

form_dropdown('size', $data_array, 'large');
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Trip_model extends CI_Model{

    var $table = 'tbl_trip';

    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function get_all_trips(){
        $this->db->from('tbl_trip');
        $query=$this->db->get();
        return $query->result();
    }

    public function get_by_id($id){
        $this->db->from($this->table);
        $this->db->where('trip_id',$id);
        $query = $this->db->get();
        return $query->row();
    }

    public function trip_add($data){
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function trip_update($where, $data){
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }

    public function delete_by_id($id){
        $this->db->where('trip_id', $id);
        $this->db->delete($this->table);
    }
}

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