Welcome Guest, Not a member yet? Register   Sign In
Question about form_dropdown
#1

[eluser]steve hunter[/eluser]
I have a question about using form_dropdown.

The code below works, but i am unsure about if I have to do the new array in the view or is there a better way of doing it with the array $games - passed from $data['games']

Should I be doing all the processing in the controller and sending over a ready array to populate the dropdown?

I tried this in the view:
Code:
echo form_dropdown('games', $games);
but got the error
Quote:Object of class stdClass could not be converted to string
I think it's because its an array of objects I have convert it?


TABLE: GAMES
Quote:GM_ID - int
GM_NAME - var

MODEL
Code:
<?php
class Test_model extends CI_Model {

   function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    function get_game_names()
    {
        $queryg = $this->db->query("SELECT * FROM games");
        return $queryg->result();
    }
}

CONTROLLER
Code:
<?php
class Test extends CI_Controller {

  public function index()
  {

    $this->load->model('test_model');
    $data['games'] = $this->test_model->get_game_names();

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

}

VIEW
Code:
$this->load->helper('form');
        echo form_open('send');
      
        $list = array();  //is this the best way to do it??
        foreach($games as $row)
        {
          $list[$row->GM_ID] = $row->GM_NAME;    //is this the best way to do it??
        }
        
        echo form_dropdown('games', $list);  //then pass this array?

        echo form_close();




Theme © iAndrew 2016 - Forum software by © MyBB