Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Adding Value to Fieldset-Select
  • Just a little question. Lets assume i have this Field here:


    $field->add('groupname', __('Select group: '), array('type' => 'select', 'options' => $groupname, 'class' => "form-control"), array(array('valid_string', array('alpha', 'numeric', 'dots', 'spaces'))));

    How can i add an additional ('options' => $groupname, 'value' => $groupId) behind that? So that every select that i add over the $groupname-array gets its responding value from another array/from the same?

    Help would be appreciated.
  • I don't understand that you mean. "behind what"? Adding a select over an array?
  • Hi James,

    I am new on fuelphp, but accordding to the docs () and if i can do understand it,
    the code your are need is

    $field->add(    'groupname',
                    __('Select group: '),
                    array(    'type' => 'select',
                            'options' => "$groupname",
                            'value' => "$groupId",
                            'class' => "form-control"
                            ),
                    array(
                        array('valid_string',
                            array(    'alpha',
                                    'numeric',
                                    'dots',
                                    'spaces'
                            )
                        )
                    )
                );

    Regards
  • Already tried that. The problem is, that when i add the value after my options, the value starts at 0 and just counts up. So lets say i add a foreach and give my groupnames + theyre corresponding id´s each separately an array:

    $group = Model_Usergroup::find('all');
            foreach($group as $group) {
                $groupname[] = $group->name;
                $groupid[] = $group->id;
    }

    Now i have an Array $groupname with all the names for the options and an array called $groupid who has the values for each group. So just from the view of code, i would get the corresponding value in the same order than the groupname.

    The only thing i want is, that when the admin created a new usergroup, he can later on change all the rights that are given to this usergroup via the adminarea. So i would need to select the group beforehand and with the id thats stored inside the select i would then go to the grouprightedit-site to let him edit this group. But for that, i would need to store the id, because it can be that the groups are called the same and so i would have problems later on with finding the right group.

    To make it short: how can i add the options-value for a select when i wanna use arrays to give the values to each option?

    With greetings.
  • HarroHarro
    Accepted Answer
    If you want to populate a dropdown from a database table, it's better and faster to use a DB query:

    // start the dropdown with a "please select...." placeholder
    $data = array('' => __('global.please-select'));

    // fetch the dropdown data
    $data += \DB::select('id', 'name')->from(\Model_Usergroup::table())->order_by('name', 'ASC')->execute()->as_array('id', 'name');

    $form->field('group_id')->set_options($data);
  • Thats one way, i just dont like the idea (my preferred style) to use different methods to retrieve Data from the Database. I would like 1 method (Like Model_Crud etc.)  and not different mixed together. But atleast this seems to work. Other way would be to not use Fieldset but to manually add the Form to the .php-file and then use some basic knowledge.

    But hey, works. So thank you.
  • Entirely up to you. 

    Both the ORM and Model_Crud are made for crud operations, and aren't suitable for fetching specific data sets quickly. Both are object based, not data based, and ORM caches as well, taking a lot of memory if you are not careful.

    Both also don't have the option to return key-value pairs, required for the dropdown option array.

    Add this code to the model using a static method, so you can use Model_Usergroup::get_groups() or something, so it's abstracted away, and you still have all database interaction in one place, in the model.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion