HawkB
HawkB

Reputation: 181

Load data to dropdown in codeigniter

I'm trying to load data in the form_dropdown. the object fornecedores has the data. How do i load and array and put into the dropdown

   <?php 

      foreach ($fornecedores as $fornecedor) {

      }


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

here's my model, where i load my object fornecedores:

public function getRecords() {

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

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

Upvotes: 0

Views: 79

Answers (2)

T. AKROUT
T. AKROUT

Reputation: 1717

The right way to use form_dropdown function

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

Upvotes: 1

user4419336
user4419336

Reputation:

Hope this helps.

Model result_array();

public function getRecords() {

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

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

On your controller you can try it like

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

}

View

<?php echo $dropdown;?>

Upvotes: 0

Related Questions