0

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

2 Answers 2

1

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"');
Sign up to request clarification or add additional context in comments.

3 Comments

Object of class stdClass could not be converted to string
Can you give me fields of fornecedore object that you want to put in the dropdown?
You should replace id and name fields by fields that you want to put in dropdown
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.