Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:02:22+00:00 2026-05-24T01:02:22+00:00

I have a question about using form_dropdown() . The code below works, but i

  • 0

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: echo form_dropdown('games', $games); but got the error “Object of class stdClass could not be converted to string”, I think it’s because its an array of objects and I have to convert it?

TABLE: GAMES

GM_ID – int

GM_NAME – var

MODEL:

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

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

$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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:02:22+00:00Added an answer on May 24, 2026 at 1:02 am

    You are correct that it needs to be converted from an object to an array, the keys are your input values, and the array values are the text displayed in the <option> when you use form_dropdown(). The way you are doing it is fine, and I personally recommend it.

    Reason: The form controls and HTML/text output are view logic. For instance, what if you want to do this instead?:

    $list[''] = 'Please select a game';
    foreach($games as $row)
    {
        $list[$row->GM_ID] = ucfirst(htmlspecialchars($row->GM_NAME));
    }
    

    That’s just my perspective, it really doesn’t matter too much, but usually as a rule – HTML and presentation logic should be in the view.

    Aside: your function name get_game_names() is confusing because it returns more than just names. Also for simplicity’s sake, you’re using ActiveRecord so you can just do this:

    function get_games() 
    {
      return $this->db->get('games')->result();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have a small question about using jquery. I have the following code: <html>
I have a question about using streams in .NET to load files from disk.
I have a question about using os.execvp in Python. I have the following bit
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
Hey So I have a question about posting using an XMLHttpRequest. In theory, if
Hi I have a question about using this pattern. When making listeners should the
I have a question about how to configure my web site and IIS using
I have a question about aspnet_applications table I have created asp.net membership using .net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.