I don't recommend your Swiss army knife get() method. While it affords much utility, your codebase will struggle to know if the returned payload will be null, a flat object, a flat array, an array of arrays, or an array of objects.
If you intend to keep your model method, it can be distilled to this:
/** * @param int $id while restricted to integers, column names cannot be passed in therefore preventing scalar return types * @param bool $object * @return (object|array|null)|(object[]|array[]) */ public function get(int $id = 0, bool $object = true) { $type = $object ? 'object' : 'array'; if ($id) { // return row as a nullable-object or a nullable-array return $this->db ->get_where($this->tbl_name, ['id' => $id]) ->row(0, $type); } // return an array of zero or more objects or arrays return $this->db->get($this->tbl_name)->result($type); }
Your controller method is calling $data['clubname'] = $this->club_model->get();. I presume your controller body resembles this:
$this->load->model('club_model'); // if not already loaded $this->load->helper('form'); // if not already loaded $data['clubname'] = $this->club_model->get(); $this->load->view('club_form', $data);
Loading the form helper gives access to the set of form helpers in the view. Passing $data['clubname'] to the loaded view will allow access to the 2d array of objects via $clubname.
As for CodeIgniter's helper function form_dropdown(), its signature is:
function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
$options is expected to be an associative array. The keys will be the value attribute values and the values will be the visible text in the option tags. If one of the element values is an array, then that subarray will be written as sub-options within a <optgroup> and its label will be the key from its parent level element.
Your view will need to convert the 2d array into a flat associative array - array_column() is perfect for this. Use the name column as values and the id column as keys.
To associate the form label with the form field, the label's for attribute must be declared and match the form field's id attribute (not the name attribute).
<tr> <td> <?php echo form_label( 'Club Name: ', // visible text 'clubname' // 'for' attribute ); ?> </td> <td> <?php echo form_dropdown( 'clubname', // 'name' attribute of select tag array_column($clubname, 'name', 'id'), // 'value' and visible text for option tags [], // selected attribute on option tags ['id' => 'clubname'] // 'id' attribute of select tag ); ?> </td> <td> <?php echo form_error('clubname'); ?> </td> </tr>
p.s.
If I wanted to better name the parameters and provide docblock annotations, that would probably look like this:
/** * Generates a <select> tag and its child elements. * @param array<string,mixed>|string $attributes Either as a flat assoc array of attributes or a name attribute for the `<select>` * @param array<string,string|array<string,string>> $options Either as a flat assoc array of `value` values => visible text for `<option>` tags or a 2d Assoc array of `<optgroup>` text => [`value` values => visible text], ... for `<option>` tags * @param string|int|array<int|string> $selectedValues Either a scalar value or a flat indexed array of values that should be marked `selected` * @param string|array<string,string> $extraAttributes Additional `<select>` attributes, either as a string (e.g. `'id="foo" data-toggle="bar" class="selectpicker form-control"'`) * or a flat assoc array of attribute => value * @return string A `<select>…</select>` HTML string */ function form_dropdown( array|string $attributes = '', array $options = [], string|int|array $selectedValues = [], array|string $extraAttributes = '' ): string