Welcome Guest, Not a member yet? Register   Sign In
object to array as in db
#1

[eluser]Pana_Ruplahlava[/eluser]
Ok, here is the situation. I can do it in php easily and i repeat it often, so there must be an easy way to do it in CI, but i dont know how. I started using it yesterday.

So: in db i have field id and field label and i need it put it into select. as id as value and label as text.

in past 2 hours in user guide i was not able to figure it out.
i assume i have to put it in array so form_dropdown can use it. but how. there must be an easy way... anybody can help? Smile

this is what i was able to get from DB Big Grin
Code:
Array
(
    [0] => Array
        (
            [label] => Fotogalerie
            [id] => 2
        )

    [1] => Array
        (
            [label] => asdasdasd
            [id] => 4
        )

    [2] => Array
        (
            [label] => asdg
            [id] => 5
        )

)


and i need it looking like

Code:
Array
(
'2'=>'Fotogalerie',
'4'=>'asdasdasd',

...
)

Thx for any help Smile
#2

[eluser]CroNiX[/eluser]
You just have to format it.

Code:
$options = array();

foreach($your_array as $arr)
{
  $options[$arr['id']] = $arr['label'];
}

Now $options is in the format needed for form_dropdown().

Code:
Array(
  [2] => 'Fotogalerie',
  [4] => 'asdasdasd',
  [5] => 'asdg'
)
#3

[eluser]Pana_Ruplahlava[/eluser]
Thanks, i had it this way, but my experience with CI is just amazing so i expected something like
Code:
do_magic_assign($array,'id','label');

Thanks a lot Smile
#4

[eluser]CroNiX[/eluser]
I created a helper, array_to_dropdown($array, $key_field, $value_field, $default = array()), that does that, as well as create an optional default first value, "Choose City" or whatever. Maybe I should submit it, although it's pretty simple like you showed.




Theme © iAndrew 2016 - Forum software by © MyBB