0

I have the following array data that is being fetched from a table that isn't mine.

Array
(
    [0] => Array
        (
            [sys_sbu] => Array
                (
                    [sbu_id] => EO
                    [sbu_name] => Executive Officer
                )

        )

    [1] => Array
        (
            [sys_sbu] => Array
                (
                    [sbu_id] => FIN
                    [sbu_name] => Finance
                )

        )

    [2] => Array
        (
            [sys_sbu] => Array
                (
                    [sbu_id] => G01
                    [sbu_name] => TEST GROUP
                )

        )

    [3] => Array
        (
            [sys_sbu] => Array
                (
                    [sbu_id] => MAF
                    [sbu_name] => Medical Affairs
                )

        )

    [4] => Array
        (
            [sys_sbu] => Array
                (
                    [sbu_id] => PCS
                    [sbu_name] => Patient Care Services
                )

        )

    [5] => Array
        (
            [sys_sbu] => Array
                (
                    [sbu_id] => SSS
                    [sbu_name] => Strategic Support Services
                )

        )

)

Basically, the table sys_sbu has 2 columns. sbu_id and sbu_name. Using the following code on my view doesn't seem to yield the expected result.

echo $this->Form->input('Groups', array(
    'type'=>'select',
    'options' => $groups)
);

enter image description here

What I want to happen is the value of each select option will be the sbu_id while the ones being displayed to the selection is the sbu_name. Like so:

<select>
  <option value="EO">Executive Officer</option>
  <option value="FIN">Finance</option>
  <option value="etc">etc</option>
</select>

How do I achieve this?

1 Answer 1

1

Normally, a $this->YourModel->find('list') would return the appropriate array if your displayField and primaryKey are set correctly in the model definition.

If for some reason they're not, you have to manually adjust your select array to look like this

array(
    [id1] => name1,
    [id2] => name2,
    etc
)

But do look into find('list'), because on 95% of the cases, it solves this kind of "problem". Just re-read that it is not your table, so I guess you're not doing a find at all. The solution remains, though, do a for in the array you're getting from the other table and adjust it like I indicated before, ids as keys, names as values

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. You are a ray of light among these deadlined times. I did use the primaryKey and displayField approach. It isn't my table but I made a model for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.