php CodeIgniter 从数据库中的选择框中获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14278765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:59:46  来源:igfitidea点击:

CodeIgniter get values from database in select box?

phpjqueryajaxcodeigniterdrop-down-menu

提问by mynameisjohn

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:

我是 CodeIgniter 的新手,所以我不知道该怎么做。我想在选择框中动态显示值,在选择值后显示一个文本框,然后将文本框值和选项(显示在下拉列表中的名称)id 传递给控制器​​,所以简单地说我想要什么去做:

  • 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
  • 动态显示选择框中的值
  • 选择值后动态创建文本框
  • 将选定的或跟踪下拉列表的“id”和文本框值传递给控制器

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

回答by moveax

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:

这是一个关于使用 jQuery、Ajax 和 Codeigniter 的教程:

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

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

回答by Khandad Niazi

after loading form helper class, your view should be for creating dropdown

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

form_dropdown('size', $data_array, 'large');

回答by Harsh Lad

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