从数据库获取值到组合框的问题

时间:2013-07-11 05:49:04

标签: mysql codeigniter

这是我的控制者:

<?PHP
class combobox extends CI_Controller 
{

    function dynamic_combobox()
    {

        $this->load->model('combobox_model');       
        $data['college'] = $this->combobox_model->getcollege();
        $this->load->view('header', $data); 
        $this->load->view('left_menu', $data);  
        $this->load->view('manage_user', $data);    
        $this->load->view('footer', $data); 
    }

}

?>

这是我的模特:

$this->db->get('colleges'); 
$data = array(); 
if ($query->num_rows() > 0) { 
    foreach ($query->result_array() as $row){ $data[] = $row; } 
} 
$query->free_result(); 
return $data;

这是我的观点:

<form action="<?php echo site_url(''); ?>" method="post">
    College

    <select name="id" id="id" style="width: 350px;">
        <option class="droplist">Please select colleges</option>
        <?
            if (count($college)>0) 
            {

                foreach ($college as $row) 
                {
                    echo "<option value='". $row['college_id'] . "'>" . $row['college_name'] . "</option>";
                }
            }
        ?>

    </select>
    Courses
    <select>
        <!--<option>UG</option>
        <option>PG</option>-->
    </select>
    <input class="button" type="button" value="Refresh"/>
</form>

2 个答案:

答案 0 :(得分:0)

我认为您错过了在模型中添加$query

$query=$this->db->get('colleges'); //<---- here notice $query=
$data = array(); 
if ($query->num_rows() > 0) { 
  $data=$query->result_array();  //<--no need of using loop as you are already looping it in you view
} 
$query->free_result(); 
return $data;

答案 1 :(得分:0)

尝试是否有效,您的型号:

$rs = $this->db->get('colleges'); 
$data = array(); 
if ($query->num_rows() > 0) { 
    $data   = $rs->result_array();
} 
$query->free_result(); 
return $data;

您的观点:

if (count($college)>0){
    foreach ($college as $row){
    echo "<option value='". $row['college_id'] . "'>" . $row['college_name'] . "</option>";
    }
}

编辑1:

$arr[]    = array('college_id' => '1', 'college_name' => 'Nadar Saraswathi College of Engineering and Technology', 'status' => '1', 'created_by' => '1', 'modified_by' => '1', 'created_date' => '2012-09-30 22:51:25', 'modified_date' => '2012-09-30 22:50:28' );
print_r($arr);
echo "<select>";
foreach ($arr as $row){
    echo "<option value='". $row['college_id'] . "'>" . $row['college_name'] . "</option>";
    }
echo "</select>";