0

我想将此查询结果显示为 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(); ?>

你能告诉我如何解决这个问题吗?这将非常有帮助。提前致谢。

4

2 回答 2

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(); ?>
于 2013-05-01T10:54:04.900 回答
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);
于 2013-05-01T11:05:03.177 回答