Forum

Thread tagged as: Question, Shop

for loop in templates

Hi!

I'm working with the shop app, and in my case, for each room, the client specifies the max number of beds that the room can have. Like this:

<input type="number" id="perch_adultBeds" name="perch_adultBeds" value="2" required="" aria-required="true" class="number input-simple ">

Now I want to provide a select tag where the buyer picks the number of beds that he wants, so was wondering if perch allows me to do something like this:

<div class="col checkAdult searchItem">
                            <div style="margin-right: 0px;">
                                <select name="Adultos" style="height:44px;background:transparent;width: calc(100% - 5px);">
                                    <perch:for id="adultBeds">
                                        <option value="<perch:for id="perch_item_index"/>"><perch:for id="perch_item_index"/>Adultos</option>
                                    </perch:for>

                                </select>
                                <img class="selectorArrow" src="./images/selectArrow.png" width="12px" height="6px">
                            </div>  
                        </div>

I used <perch:for> just as an example, but the final result would be a select tag filled with all the possible options (in this case from 1 to 2). I couldn't find anything like this (I tried repeaters and blocks don't seem to be what im looking for ).

The only way within perch that I'm seeing is by fetching the number of beds beforehand, then in my php code generate the template, and then pass it as a variable to the template.

What would you advice?

thanks

Gustavo Bica

Gustavo Bica 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Gustavo,

If you use the input select field, your options can be like this:

<perch:input id="Adultos" name="Adultos" type="select" options="1 Adultos|1,2 Adultos|2,3 Adultos|3" >

You can generate the options in PHP with the each option:

perch_shop_product('my-product', [
  'each' => function($item) {
    $item['adultos_opts'] = // generate your options from $item['adultBeds'] in the format 1 Adultos|1,2 Adultos|2
    return $item;
  }
]);

And then use adultos_opts as the value of the options attribute:

<perch:input id="Adultos" name="Adultos" type="select" options="<perch:shop id="adultos_opts">" >

Thank you Hussein Al Hammad, I appreciate it :)