2017-05-31 76 views
0

我正試圖加載form_dropdown中的數據。對象fornecedores有數據。如何加載和數組並投入下拉將數據加載到codeigniter中的下拉列表中

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

回答

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"'); 
+0

'類stdClass的客體不能轉換到string' – HawkB

+0

你能給我fornecedore對象的領域你想放在下拉菜單中? –

+0

你應該通過你想放在下拉列表中的字段替換id和名稱字段 –

0

希望這會有所幫助。

Model 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;?> 
相關問題