将数据加载到codeigniter中的下拉列表

时间:2017-05-31 01:43:40

标签: php codeigniter

我正在尝试在form_dropdown中加载数据。对象fornecedores具有数据。如何加载和数组并放入下拉列表

   <?php 

      foreach ($fornecedores as $fornecedor) {

      }


      echo form_dropdown('fornecedores', null, 
    set_value('fornecedores'),  ['class' => 'form-control']);
     ?>

这是我的模型,我加载了我的对象fornecedores

public function getRecords() {

    $query = $this->db->get('fornecedores');

    if ($query->num_rows() > 0) {
        return $query->result();
    }
}

2 个答案:

答案 0 :(得分:1)

使用form_dropdown函数的正确方法

https://www.codeigniter.com/userguide3/helpers/form_helper.html

$options = array();
foreach ($fornecedores as $fornecedor) {
    $options[$fornecedor->id] = $fornecedor->name;
}

echo form_dropdown('fornecedores', $options, null,  'class="form-control"');

答案 1 :(得分:0)

希望这有帮助。

模型result_array();

public function getRecords() {

    $query = $this->db->get('fornecedores');

    if ($query->num_rows() > 0) {
        return $query->result_array();
    }
}

在您的控制器上,您可以尝试

public function index() {

$this->load->model('some_model');

$options = array();

$fornecedores = $this->some_model->getRecords();

foreach ($fornecedores as $fornecedor) {
    $options[$fornecedor['id']][] = $fornecedor['name'];
}

$data['dropdown'] =  form_dropdown('fornecedores', $options, '',  array('class' => 'form-control'));

$this->load->view('some_view', $data);

}

查看

<?php echo $dropdown;?>