如何在form_dropdown中显示db结果

时间:2013-05-01 10:47:51

标签: php codeigniter

我想将此查询结果显示为form_dropdown

模型

public function deptlist_view() {
    $this -> load -> database();
    $query = $this -> db -> query('select var_staffdepartment from tbl_admin');
    return $query -> result();

}

控制器

public function view_dept() {
    $this -> load -> model('model_database');
    $deptdata['query'] = $this -> model_users -> deptlist_view();

    $this -> load -> view('signup', $deptdata);

}

查看

     <?php $dept= ????  ?>



        <?php echo form_open('welcome/signup_validation'); ?>

         <table align="center">


    <tr>
   <td>
    <p> Departments :</p>
    </td>
            <td><?php  echo form_dropdown('staffdept', $dept); ?></td>


                        </table>

                    <?php echo form_close(); ?>

您能否说明如何解决此问题?这将是非常有帮助的。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您需要传递数组中的下拉选项,如..

 $options = array(
               'small'  => 'Small Shirt',
              'med'    => 'Medium Shirt',
              'large'   => 'Large Shirt',
              'xlarge' => 'Extra Large Shirt',
            );

所以我创建了一个新数组..以正确的数组格式添加选项并在视图中显示

试试这个

<强>控制器

public function view_dept() {
  $this -> load -> model('model_database');
  $query = $this -> model_users -> deptlist_view();
  $tempArray=array();
  foreach($query as $row){
     $tempArray[$row->var_staffdepartment]=$row->var_staffdepartment;
  }
  $data['department']=$tempArray;
   $this -> load -> view('signup', $data);

}

查看

 <?php echo form_open('welcome/signup_validation'); ?>

  <table align="center">
  <tr>
    <td>
      <p> Departments :</p>
    </td>
    <td><?php  echo form_dropdown('staffdept', $department); ?></td>
   </table>

    <?php echo form_close(); ?>

答案 1 :(得分:0)

控制器:

public function view_dept() 
{
  $this->load->model('model_database');
  $query = $this->model_users->deptlist_view();
  $data['staffdept'] = $query->result(); //
  $this->load->view('signup', $data);
}

查看:

<?php
    $options = array('Select a department'); 
    foreach ($staffdept as $staff):
        $options[$staff->ID] = $staff->name; //insert the sql query results here
    endforeach;
    echo form_dropdown('staffdept', $options, $staff->ID);
?>

要在下拉列表中添加类或ID,请编辑代码中的最后一个“echo form_dropdown”行

$other = "id='staffdept', class='staffdept'";
echo form_dropdown('staffdept', $options, $staff->ID, $other);