Welcome Guest, Not a member yet? Register   Sign In
Smartest way to build an HTML select
#1

[eluser]IFR70[/eluser]
Hi all,
I'm a beginner in PHP and Codeigniter, so please apologize if my question is very basic.

I have two tables like these:

The first table name is "friends" and the columns are:
- ID_friend (int)
- Name (varchar(50))
- Surname (varchar(50))
- ID_nation (int)

The second table is "nation" and the columns are:
- ID_nation (int)
- Nation_name (varchar(50))

OK, now I have to build a form to insert a record on the table "friends", but I want to decode the field "ID_nation" with all the "Nation_name"s of the second table.

In my model I have this function:

Code:
function listColumns()
    {
        $fields = $this->db->field_data($this->table);
        return $fields;
    }

The array returned by this function is this one:

Code:
Array
(
    [0] => stdClass Object
        (
            [name] => ID_friend
            [type] => int
            [default] =>
            [max_length] => 11
            [primary_key] => 1
        )

    [1] => stdClass Object
        (
            [name] => Name
            [type] => varchar
            [default] =>
            [max_length] => 50
            [primary_key] => 0
        )

    [2] => stdClass Object
        (
            [name] => Surname
            [type] => varchar
            [default] =>
            [max_length] => 50
            [primary_key] => 0
        )

    [3] => stdClass Object
        (
            [name] => ID_nation
            [type] => int
            [default] => 0
            [max_length] => 11
            [primary_key] => 0
        )

)

Is there a smart way to substitute ID_nation with the list of the nations in the second table, so when I pass the array to the view I can easily build an HTML SELECT?

Cheers.
#2

[eluser]InsiteFX[/eluser]
See the CodeIgniter Form_helper
#3

[eluser]IFR70[/eluser]
Thank you for the answer, InsiteFX.

I apologize but I'm a true beginner, so your tip it's not really clear for me. I've read the CodeIgniter Form_helper, and I use this helper to build forms, but I'm not sure if I understood how I can easily read the nations values to build the select.

The only one way I can imagine is to add to the array $fields the result of:

Code:
select Nation_name from nation

and then use the form_dropdown() within the view... is it correct?

Smile
#4

[eluser]InsiteFX[/eluser]
Yes, you need to build the array for the dropdown like so.

This can come from a database etc;

Code:
$data = array(
    ''   => 'Choose',    // this can be named what you want it is the default selection...
    'United States' => 'United States',
// or use a number
    '1' => 'Value',
);

Example for a Month's array.
Code:
$data = array(
   ''   => 'Month',
   '1'  => 'January',
   '2'  => 'Febuary',
   '3'  => 'March',
   '4'  => 'April',
   '5'  => 'May',
   '6'  => 'June',
   '7'  => 'July',
   '8'  => 'August',
   '9'  => 'September',
   '10' => 'October',
   '11' => 'November',
   '12' => 'December',
  );
#5

[eluser]IFR70[/eluser]
Thank you very much.

Smile




Theme © iAndrew 2016 - Forum software by © MyBB