3

I am new in CodeIgniter and working on a application in which i have a form. In this form I have a dropdown. I have some values in a column of database. I want these values in the dropdown, I have no idea how to do it.

My view is:

<tr> <td>Moderator :</td> <td><label for="select"></label> <select name="mod" tabindex="1" > <!--values i want from database--> </select> </td> </tr> 
6
  • Do you know how to fetch data with a model and pass that data to your view? Commented Mar 8, 2013 at 11:29
  • yeh i know . . @FabioAntunes Commented Mar 8, 2013 at 11:36
  • The first thing you need to do is to create a model to allow you to extract the data you need from the database. It sounds like you don't know anything about MVC so I would suggest studying this first. Commented Mar 8, 2013 at 11:39
  • @Pattle :- i know about MVC i already create a model and fetch data from database but there is a problem with dropdown list . . Commented Mar 8, 2013 at 11:41
  • @Pattle:- let me explain . . i have one field named member_name i want to use it as moderator_name also..i give a dropdown in which i want to set this name and by selecting this new members created . . Commented Mar 8, 2013 at 11:44

2 Answers 2

1

This example is for a country table in our application, the table contains, id, symbol, name

Controller:

$data['countries']=$this->countries_model->get_countries(); $this->load->view('countries',$data); 

Model:

 function get_countries() { $query = $this->db->get('countries'); if ($query->num_rows >= 1) { foreach($query->result_array() as $row) { $data[$row['countryId']]=$row['countryName']; } return $data; } } 

It returns an array like:

$id=>$name 0=>Canada 1=>United States 

View:

echo form_dropdown('countries',$countries); //ci syntax 

Or:

<select name="country"> <?php foreach($countries as $country) { echo '<option value="'.$country['id'].'">'.$country['name'].'</option>'; } ?> </select> 
Sign up to request clarification or add additional context in comments.

Comments

0
<?php class Member_model extends CI_Model{ var $table = 'tbl_member'; public function __construct(){ parent::__construct(); $this->load->database(); } public function get_all_members(){ $this->db->from('tbl_member'); $query=$this->db->get(); return $query->result(); } public function get_by_id($id){ $this->db->from($this->table); $this->db->where('member_id',$id); $query = $this->db->get(); return $query->row(); } public function member_add($data){ $this->db->insert($this->table, $data); return $this->db->insert_id(); } public function member_update($where, $data){ $this->db->update($this->table, $data, $where); return $this->db->affected_rows(); } public function delete_by_id($id){ $this->db->where('member_id', $id); $this->db->delete($this->table); } } 

1 Comment

Can you add some explanation to your code, such that others can learn from it? For example, how does it populate the select field that was asked for in the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.