Welcome Guest, Not a member yet? Register   Sign In
How to populate a dropdown from db with codeigniter?
#1

[eluser]PlesaCarmen[/eluser]
Hi, I am new in codeigniter, I want to populate a dropdown from database and i don't know how to start.

I have 10 tables in a database printer.Every tables has a type, price and id.

My questions is how to populate each table in a dropdown?

I using the code and I get this error:

My model:

class Model_print_run extends CI_Model{


public function get_dropdown()
{
$result=$this->db->get('print_run');
$dropdown=array();
foreach($result->result_array() as $row)
{
$dropdown[$row['id']]=$row['type'];
}
return $dropdown;
}



}

My controller:

class print_run extends CI_Controller{

public function index()
{

$this->load->model('model_print_run');
$data['print_run']= $this->model_print_run->get_dropdown();
$this->load->view('print_run_view', $data);
}
}

My view:
echo form_dropdown('print_run', $dropdown);



I using the code and I get this error:
Severity: Notice

Message: Undefined variable: dropdown

Filename: views/print_run_view.php

Line Number: 2

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

Thank you!

Any suggest will be help!

#2

[eluser]skunkbad[/eluser]
Use code tags
#3

[eluser]PlesaCarmen[/eluser]
Will you be more explicitly please?

I also want to have an option value from each table.Option value is the price and then I want to calculate the total price from each item selected.

Will you help me please?

Thank you!
Code:
class Model_print_run extends CI_Model{
  

     public function get_dropdown()
  {
    $result=$this->db->get('print_run');
    $dropdown=array();
   foreach($result->result_array() as $row)
     {
     $dropdown[$row['id']]=$row['type'];
     }
     return $dropdown;
  }


    
}

My controller:

class print_run extends CI_Controller{

  public function index()
{
  
   $this->load->model('model_print_run');
   $data['print_run']= $this->model_print_run->get_dropdown();
   $this->load->view('print_run_view', $data);
}
}

My view:

echo form_dropdown('print_run', $dropdown);

Any advice will be help!

I get the error:

Message: Undefined variable: dropdown

Message: Invalid argument supplied for foreach()

Quote:
#4

[eluser]skunkbad[/eluser]
Code:
public function get_dropdown()
  {
    $result=$this->db->get('print_run');
    $dropdown=array();
    if( $result->num_rows() > 0 )
    {
     foreach($result->result_array() as $row)
       {
       $dropdown[$row['id']]=$row['type'];
       }
    }
    return $dropdown;
  }

The real problem is that your query is not returning any results, so the dropdown will not be populated. Try using $this->db->last_query(); after the query and run that sql manually in phpMyAdmin or your MySQL client.
#5

[eluser]PlesaCarmen[/eluser]
If I use this line

return $dropdown->result_array();

I get this error:

Cannot use object of type stdClass as array

Thanks!
#6

[eluser]PlesaCarmen[/eluser]
ok, I find the error:

echo form_dropdown('dropdown', $print_run);

Thank you for you time Smile




Theme © iAndrew 2016 - Forum software by © MyBB