looping through an array

edited December 2011 in Modules
Hi after looking hi and low I cannot find out how to do the following:

'select', 'options' => array('hawker' => '1'),

I am wanting to populate the options with data from the database. Just like you would conventionally with:

THE LOOP {

$row['name']

}

This would automatically fill the dropdown with data from the database....

How would I do this with the array structure below?

thanks



My code below:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Aircraft_model extends Base_module_model {

function __construct()
{
parent::__construct('aircraft');
}

function form_fields($values=array()) {

// LOOP THROUGH THE FLEETS
$query = $this->db->query("SELECT * FROM fleets ORDER BY fname ASC");
$rows = $query->result_array();


$fields = parent::form_fields($values);
$fields['areg'] = array('type' => 'input', 'label' => 'Aircraft Registration', 'required' => TRUE);
$fields['fleet_id'] = array('name' => 'Aircraft Fleet', 'type' => 'select', 'options' => array('hawker' => '1'), 'required' => TRUE);
$this->form_builder->render_divs($fields);
return $fields;
}

}

Comments

  • edited 11:24AM
    The model's "options_list()" method is good for dropdowns:
    http://www.getfuelcms.com/user_guide/libraries/my_model/table_class_functions

    In your case you could try the following:
    $options = $this->options_list('id', 'name');

    Also, it looks like fleet_id is a foreign key to another table, so if you set the following model property like so, the dropdown will automatically be set for you:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br /> class Aircraft_model extends Base_module_model { public $foreign_keys = array('fleet_id' => 'fleets_model'); function __construct() { parent::__construct('aircraft'); }

    If the model is in a different module, the syntax would be the following:
    public $foreign_keys = array('fleet_id' => array('my_module' => 'fleets_model'));
  • edited 11:24AM
    $options = $this->options_list('id', 'name');

    this does work nice, however its grabbing the data from the aircraft table when I need to grab it from the fleets table. Do i need to add another param?
  • edited 11:24AM
    You need to load that model and call that method on the model:
    $this->load->model('fleets_model'); $options = $this->fleets_model->options_list('id', 'name');
    Using the foreign key property will automatically do this for you though too.
  • edited 11:24AM
    Works! Thankyou very much
Sign In or Register to comment.