0
votes

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>
1
You should have 2 queries, one that populates an array with all animals from you database, which you then loop in twig to fill your select and another to get all the details of the selected animal. Imo you should use the id of an animal, instead of its type as value in the select.DarkBee
How would I populate the animal array, would this require a separate field? The pet field in the database is currently an ENUM which holds these pet names, also would it be possible for you to provide an example to as how you would go about doing your suggestion in a twig syntax?mhvvzmak1

1 Answers

1
votes

Following code is a just an example on how to achieve someting you want. I did not test it

tables

---------------
| animal_type |
---------------
id [PK]
title


---------------
|   animals   |
---------------
id [PK]
name
animal_type_id [FK, animal_type.id]
breed
birthdate
...

php

<?php
    $animal_types = $animals = [];
    $dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $pass);
    //Raise exception on errors
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //Target all available types:
    $stmt_types = $dbh->prepare('SELECT id, title FROM animal_type ORDER BY title');

    if ($stmt_types->execute()) {
        $animal_types = $stmt_types->fetchAll();
    }

    if (isset($_POST['cbo_animal_type']) && is_numeric($_POST['cbo_animal_type'])) {
        $stmt_animals = $dbh->prepare('SELECT * FROM animals WHERE animal_type_id = :animal_type_id ORDER BY name');
        if ($stmt_animals->execute()) {
            $animal_types = $stmt_animals->fetchAll([ 'animal_type_id' => $_POST['cbo_animal_type']);
        }       
    }

    echo $twig->render('path/to/view.html', [
        'animal_types'          => $animal_types,
        'animals'               => $animals,
        'selected_animal_type'  => isset($_POST['cbo_animal_type']) && is_numeric($_POST['cbo_animal_type']) ? $_POST['cbo_animal_type'] : -1,
    ]);

twig

<form method="POST">
    <select name="cbo_animal_type">
    {% for animal_type in animal_types %}
        <option value="{{ animal_type.id }}"{% if selected_animal_type is defined and selected_animal_type = animal_type.id %} selected{% endif %}>{{ animal_type.title }}</option>
    {% endfor %}    
    </select>
</form>

{% if animals is defined and not animals is empty %}
<ul>
    <li>
        <div>
            <h1>{{ animal.name }}</h1>
            <h2>{{ animal.breed }}</h2>
            <p>
                {{ animal.birthdate }}<br />
            </p>
        </div>
    </li>
</ul>
{% endif %}