php - Put specified index from an array in the top of select box

779

I have the following array:

Array
(
   [2] => Afganistan
   [3] => Africa
   [4] => Albania
   [5] => Alegira
   [6] => Turcia
 )

In the template .handlebars I do :

{{#each oForm.lCountry}}
    <option value="{{@key}}">
      {{this}}
    </option>
{{/each}}

In the first value of selectbox I getAfganistan, how to do a verification tu put on the first valueTurcia ? Please help me. Thx in advance and sorry for my english

851

Answer

Solution:

I would suggest to just mark "Turcia" as selected.

Handlebars Helper: (Javascript version)

Handlebars.registerHelper('eq', function(a, b, block) {
    return a == b ? block.fn(this) : block.inverse(this);
});

Template:

{{#each oForm.lCountry}}
    <option value="{{@key}}" {{#eq this 'Turcia'}}selected="true"{{/eq}}>
      {{this}}
    </option>
{{/each}}

Output:

<option value="2" >
    Afganistan
</option>
...
<option value="6" selected    Turcia
</option>

Now "Turcia", will be selected by default. Which is more logical so you can mantain the alphabetical order.

If you want "Turcia" to appear first by any means, just change the order of the array in PHP.

People are also looking for solutions to the problem: php - Send checked table row value in array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.