I am the Twig template engine with PHP however having issues with the rendering of certain forms, specifically automatically displaying the current selected item of a "select" input.
I am extracting certain sets of information from the database, these are: pet, breed, location which return their respective values e.g. "cat,dog".
I am not sure how to display these inputs to the user, as the input select value would differ from the text, I did think of using a swtich however wasn't sure if it was the best selection.
Here's my current code:
$select = SELECT pet, breed, location FROM...;
$pet = array('cat', 'dog');
The page then renders the view with these variables.
<select class="form-control">
{% for p in pet %}
{% if p == select.pet %}
<option value="{{ select.pet }}" selected>{{ select.pet|capitalize }}</option>
{% else %}
<option value="{{ p }}">{{ p|capitalize }}</option>
{% endif %}
{% endfor %}
As you can see I'm currently required to rely on the "pet" variable to loop through the inputs, this is what I'm trying to change, also as you can see the text of the select is just the value but capitalized meaning it is limited.
How can I create a twig loop which wouldn't require the array $pet, and would allow me to specify each of the values => text like so:
<option value="{{ select.pet }}" selected>Foo dog</option>
<option value="{{ select.pet }}">Bar cat</option>
id
of an animal, instead of its type as value in the select. – DarkBee