setara
setara

Reputation: 11

create a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:

Model

function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}

Controller

  function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}

View

 <select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>

It does not show any error and any option to show

Upvotes: 0

Views: 210

Answers (3)

Daniel Lucena
Daniel Lucena

Reputation: 11

Model

public function getClasse() {
    $query(`enter code here`);       
    $result = $this->db->query($query)->result_array();

    foreach ($result as $key => $rows) {
        $resultado[] = $rows['DescricaoClasse'];
    }
    return $resultado;
}

Controller:

public function getClasse() {
    $this->load->model('Decisao_monocratica_model');
    return $this->Decisao_monocratica_model->getClasse();
}

View

<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
    <option value="0">Todos os Tipos</option>
        <? foreach ($classeProcesso as $key => $classe) { ?>
            <option value="<? echo $classe ?>"><? echo $classe ?></option>
        <? } ?>
</select>       

Upvotes: 0

Ahmed iqbal
Ahmed iqbal

Reputation: 597

Model:

function get_sec_info(){

$this->db->select('sec_id,name');

$records = $this->db->get('section');

return $records->result();

}

Controller:

function sec_ifo(){

$this->load->model('mymodel');

$this->data['red'] = $this->mymodel->get_sec_info();

$this->load->view('article',$this->data);

}

View:

<select name="section">
<?php foreach($red as $row) { ?>
    <option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>

Upvotes: 0

Gonz
Gonz

Reputation: 1219

In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec

Upvotes: 1

Related Questions