2

I collect data from the database table and display it in the drop-down list, here's what I've done so far

THIS IS MY CONTROLLER:

public function assign_hostel() {
        $inner_page_title = 'Assign Hostel'; 
        $this->admin_header('Assign Hostel', $inner_page_title);
        $data['hostel'] = $this->hostel_model->get_hostel();    
        $this->load->view('admin/hostel/assign_hostel');
        $this->admin_footer();
    }

THIS IS MY MODEL:

public function get_hostel() { //get all hostel
        $this->db->order_by('hostel_name', 'asc');
        return $this->db->get_where('school_hostel',  array('hostel_name' => 
        $hostel_name)->result();
    }

MY VIEW:

<div class="form-group">
        <label class="form-control-label">Select Hostel</label>
         <select class="form-control" name="state_of_origin" required>
           <option value="">-Select-</option>
             <?php 
             $hostel_name = get_hostel();
             foreach ($hostel_name as $hostel ) { ?>
           <option value="<?php echo $hostel; ?>"><?php echo $hostel; ?></option>
            <?php } ?>
        </select>
</div>

Why am I getting an empty drop-down list?

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Sylvester
  • 23
  • 3

4 Answers4

0

There are few changes you need to make:

Change 1:

Pass the $data variable when you load the view:

$data['hostel'] = $this->hostel_model->get_hostel();    
$this->load->view('admin/hostel/assign_hostel', $data); // $data as 2nd argument

Change 2:

Access key of $data array as variable in view file like this:

<div class="form-group">
        <label class="form-control-label">Select Hostel</label>
         <select class="form-control" name="state_of_origin" required>
           <option value="">-Select-</option>
             <?php 
             //$hostel_name = get_hostel(); NO NEED THIS LINE
             foreach ($hostel as $h ) { ?>
           <option value="<?php echo $h->hostel_name; ?>"><?php echo $h->hostel_name; ?></option>
            <?php } ?>
        </select>
</div>

I still need to know what columns you are fetching. Because in <option> tag you need to use the variable to access their attributes. Right now I have used the variable $h itself which is wrong.

You should use something like $h->name;

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

You can't get $hostel from view directly to model, you must get from controller and pass it via $data to view

CONTROLLER:
public function assign_hostel() {
        $inner_page_title = 'Assign Hostel'; 
        $this->admin_header('Assign Hostel', $inner_page_title);
        $data['hostels'] = $this->hostel_model->get_hostel();    
        $this->load->view('admin/hostel/assign_hostel', $data);
        $this->admin_footer();
    }

MODEL:
public function get_hostel() { //get all hostel
        $this->db->order_by('hostel_name', 'asc');
        return $this->db->get_where('school_hostel',  array('hostel_name' => 
        $hostel_name)->result();
    }

VIEW:
<div class="form-group">
        <label class="form-control-label">Select Hostel</label>
         <select class="form-control" name="state_of_origin" required>
           <option value="">-Select-</option>
             <?php 
             foreach ($hostels as $hostel) { ?>
           <option value="<?php echo $hostel; ?>"><?php echo $hostel; ?></option>
            <?php } ?>
        </select>
</div>
0

Pass $data to view function in controller CONTROLLER:

public function assign_hostel() {
    $inner_page_title = 'Assign Hostel'; 
    $this->admin_header('Assign Hostel', $inner_page_title);
    $data['hostel'] = $this->hostel_model->get_hostel();    
    $this->load->view('admin/hostel/assign_hostel', $data);
    $this->admin_footer();
}

And in view get $hostel

VIEW:

<div class="form-group">
    <label class="form-control-label">Select Hostel</label>
     <select class="form-control" name="state_of_origin" required>
       <option value="">-Select-</option>
         <?php 
         foreach ($hostel as $hostel ) { ?>
       <option value="<?php echo $hostel; ?>"><?php echo $hostel; ?></option>
        <?php } ?>
    </select>

Er. Mukesh Sharma
  • 285
  • 1
  • 4
  • 16
0

Hope this will help you :

Pass $data['hostels'] in your view as given below :

First your controller assign_hostel should be like this :

public function assign_hostel() 
{
    $inner_page_title = 'Assign Hostel'; 
    $this->admin_header('Assign Hostel', $inner_page_title);
    $data['hostels'] = $this->hostel_model->get_hostel();    
    $this->load->view('admin/hostel/assign_hostel' ,$data);
    $this->admin_footer();
}

Second your view should be like this :

<div class="form-group">
    <label class="form-control-label">Select Hostel</label>
     <select class="form-control" name="state_of_origin" required>
       <option value="">-Select-</option>
         <?php if ( !empty($hostels)) {
         foreach ($hostels as $hostel ) { ?>
            <option value="<?=$hostel->hostel_name; ?>"><?=$hostel->hostel_name;?></option>
        <?php } }?>
    </select>
</div>

For more : https://www.codeigniter.com/user_guide/general/index.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34