Codeigniter : Call Option Selected from databaseExample tables
1 imgtype : imgtype_id (pk), imgtype_name (VACHAR)
controllers prd.php
1 2 3 4 5 6 7 |
public function newdata() { $data['results'] = $this->Imgtype_model->fetch_imgtype(0,0,''); $this->load->view('template/backheader'); $this->load->view('prd/newdata',$data); $this->load->view('template/backfooter'); } |
models Imgtype_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php class Imgtype_model extends CI_Model { public $imgtype_name; //public $description; public function __construct() { parent::__construct(); } public function record_count($keyword) { $this->db->like('imgtype_name',$keyword); $this->db->from('imgtype'); return $this->db->count_all_results(); } public function fetch_imgtype($limit, $start,$keryword) { $this->db->like('imgtype_name',$keryword); $this->db->limit($limit, $start); $query = $this->db->get('imgtype'); if($query->num_rows() > 0) { foreach($query->result() as $row) { $data[] = $row; } return $data; } return FALSE; } public function entry_imgtype($imgtype_id, $typeimg = '') { $data = array( 'imgtype_name'=> $this->input->post('imgtype_name'), 'typeimg' => $typeimg ); if($imgtype_id == NULL) { $this->db->insert('imgtype', $arr); } else { $this->db->update('imgtype', $data, array('imgtype_id'=> $imgtype_id)); } } public function read_imgtype($imgtype_id){ $this->db->where('imgtype_id',$imgtype_id); $query = $this->db->get('imgtype'); if($query->num_rows() > 0){ $data = $query->row(); return $data; } return FALSE; } public function remove_imgtype($imgtype_id){ $this->db->delete('imgtype',array('imgtype_id'=>$imgtype_id)); } } |
Views form insert
1 2 3 4 5 6 7 8 9 |
<select class="form-control" name="imgtype_id"> <option value="">เลือกข้อมูล</option> <?php foreach($results as $result){?> <option value="<?php echo $result->imgtype_id; ?>"> <?php echo $result->imgtype_name; ?> </option> <?php} ?> </select> |