Reputation: 71
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:
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;?>
Upvotes: 1
Views: 24437
Reputation: 1
<?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);
}
}
Upvotes: 0
Reputation: 2396
after loading form helper class, your view should be for creating dropdown
form_dropdown('size', $data_array, 'large');
Upvotes: 0
Reputation: 133
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/
Upvotes: 3