0

I wanna display this query result as form_dropdown

Model

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

}

Controller

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

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

}

View

     <?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(); ?>

Could you please show how to fix this Problem? It would be very helpfull. thanks in advance.

2 Answers 2

1

you need to pass the dropdown options in an array like..

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

so i created a new array .. added options in correct array format and displayed in the view

try this

Controller

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

}

view

 <?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(); ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Controller:

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

View:

<?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);
?>

To add a class or ID to the dropdown, edit the last 'echo form_dropdown' line in your code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.