2

我从数据库表中收集数据并将其显示在下拉列表中,这是我到目前为止所做的

这是我的控制器:

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

这是我的模型:

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

我的观点:

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

为什么我得到一个空的下拉列表?

4

4 回答 4

0

您需要进行一些更改:

变化一:

$data加载视图时传递变量:

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

变化2:

数组的访问键$data作为视图文件中的变量,如下所示:

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

我仍然需要知道您要获取哪些列。因为在<option>标签中你需要使用变量来访问它们的属性。现在我使用了$h错误的变量本身。

你应该使用类似的东西$h->name;

于 2018-07-20T11:26:37.750 回答
0

您不能$hostel从视图直接获取模型,您必须从控制器获取并将其传递$data给视图

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>
于 2018-07-20T11:27:11.790 回答
0

将 $data 传递给控制器​​ 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();
}

并在视图中获得 $hostel

看法:

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

于 2018-07-20T11:30:21.533 回答
0

希望对你有帮助 :

传递$data['hostels']您的视图,如下所示:

首先你的控制器assign_hostel应该是这样的:

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

其次你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 if ( !empty($hostels)) {
         foreach ($hostels as $hostel ) { ?>
            <option value="<?=$hostel->hostel_name; ?>"><?=$hostel->hostel_name;?></option>
        <?php } }?>
    </select>
</div>

更多信息:https ://www.codeigniter.com/user_guide/general/index.html

于 2018-07-20T12:53:10.550 回答