We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Fill a Select element with data from an array

I have an array filled with data like this:

$results[] = ['id' => $question->id, 'question' => $question->question, 'tag' => $tag->tag];

I want to pass that data into a Select element so that the value for each option is ID and the choice displayed is the question.

Based on the documentation I have tried this:

      $select = new Select(
        'question',
        $results,
        [
          'class' => 'question-search',
          'style' => "width: 100%",
          'using' => [ $results['id'], $results['question']." ( ".$results['tag']." )"]
        ]
      );

But that way it shows each $results entry as separated choices.

And by doing this:

    foreach($results as $result)
      $select->addOption([$result['id'], $result['question']." ( ".$result['tag']." )"]);

only the last record in the array is added. Please help, what am I missing?

Thanks



32.2k
Accepted
answer

Hi @kristijanFaust you have a little error

[ $results['id'] => $results['question']." ( ".$results['tag']." )"] // show <option value="id">question</option>

You have to use a key as option value and value like anchor

Good luck

Thanks,

I tought one could pass individual values to the Select element but it seems it can not be done.