0

这是我的控制器:

<?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>
4

2 回答 2

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;
于 2013-07-11T05:57:13.497 回答
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>";
于 2013-07-11T05:57:37.460 回答