We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.3k
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.