3

我是CodeIgniter的新手,正在开发一个我有表单的应用程序。在这种形式中,我有一个下拉菜单。我在数据库的列中有一些值。我想要下拉列表中的这些值,我不知道该怎么做。

我的看法是:

<tr>
    <td>Moderator :</td>
    <td><label for="select"></label>
    <select name="mod" tabindex="1" >
    <!--values i want from database-->
    </select>
    </td>
</tr>
4

2 回答 2

1

这个例子是针对我们应用中的一个国家表,该表包含,id,symbol,name

控制器:

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

模型:

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

它返回一个数组,如:

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

看法:

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

或者:

<select name="country">
<?php
    foreach($countries as $country)
    {
       echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
    }
?>
</select>
于 2013-03-08T11:41:22.123 回答
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);
}
}
于 2018-10-04T07:07:05.793 回答