Selecting a particular Index in dropdown menu (twig)


#1

Hello all,
I am using twig in my slim app.
I have a dropdown menu that I want to select a particular index based on the value passed from the url request.

here is my dropdown code


   <div class="field select-box">
                                <select name="plan" data-placeholder="Select Your Preferred Plan">
                                    <option>Select One</option>
                                    <option value="5000">Basic Plan - ₦5,000</option>
                                    <option value="10000">Advanced Plan - ₦10,000</option>
                                    <option value="20000">Professional Plan - ₦20,000</option>
                                </select>


                                <i class="fa fa-sort"></i>
                                {% if errors.plan%}
                                    <span style="color: #ffffff;" class="help-block">{{ errors.plan| first }}</span>
                                {% endif %}
                            </div>

please how do I do this.
Thanks.


#2

If I’m understanding you properly… you would likely want to send the packages to the view as well as the selectedPlan, then loop through the plans and check to see if the package’s ID matches the user’s selected package , something like this…

<select name="package">
    <option>Select One</option>
    {% for package in packages %}
       <option value="{{ package.price }}"{% if package.id == selectedPlan %} selected="selected"{% endif %}>{{ package.name }}</option>
    {% endfor %}
</select>

#3

first of all,
thanks for replying.

something close to that.
the packages are already in the view but I want to select a package by default depending on the value I get from my request param, that is if the param says “5000”, select the first dropdown, if the param says “10000”, select the second dropdown and so on.


#4

Send the parameter to the view in whatever manner you are sending other data to your view and call it something like selectedPackage. (Or is that the part you need help with?) Then your view would look something like this.

<select name="plan" data-placeholder="Select Your Preferred Plan">
    <option>Select One</option>
    <option value="5000"{% if selectedPlan == 5000 %} selected="selected"{% endif %}>Basic Plan - ₦5,000</option>
    <option value="10000"{% if selectedPlan == 10000 %} selected="selected"{% endif %}>Advanced Plan - ₦10,000</option>
    <option value="20000"{% if selectedPlan == 20000 %} selected="selected"{% endif %}>Professional Plan - ₦20,000</option>
</select>

#5

                         
                            <div class="field select-box">
                                <select name="package" data-placeholder="Select Your Preferred Package">
                                    <option>Select One</option>

                                    {% set selectedPlan = "10000" %} // declare a twig variable to behave as value from controller

                                    <option value="5000"{% if selectedPlan == "5000" %} selected="selected"{% endif %}>
                                        Basic Plan - ₦5,000
                                    </option>
                                    <option value="10000"{% if selectedPlan == "10000" %} selected="selected"{% endif %}>
                                        Advanced Plan - ₦10,000
                                    </option>
                                    <option value="20000"{% if selectedPlan == "20000" %} selected="selected"{% endif %}>
                                        Professional Plan - ₦20,000
                                    </option>

                                </select>


                                <i class="fa fa-sort"></i>
                                {% if errors.package %}
                                    <span style="color: #ffffff;" class="help-block">{{ errors.package | first }}</span>
                                {% endif %}
                            </div>

but it doesn’t works.


#6

sorry if am such a bother,
but after my modifications above, it still does not work.


#7

Output the value of selectedPlan in your view to see if you are indeed sending it to the view.


#8

To make debugging easier, I declared selectedPlan as a local twig variable in my twig file with a value of 10000.
You can see it in my last code post.
It still does not work.


#9

Not sure what to tell you there… I copied and pasted your code into one of my Twig templates and it properly selects Advanced Plan - ₦10,000.


#10

Could you please share that templates of yours?
I must be doing something stupid.


#11

I’m not sure what you mean. I literally copied the code you last wrote above and pasted it into a template I named test.html.twig.


#12

thanks for your patience.
the issue was caused by the bootstrap theme am using.

thanks once again.


#13

Ahhh, good, glad you got it sorted out.


#14
>Disabled
                                    <option value="1" <?php echo (!empty($advanced_newsletter_send_for) && in_array(1, $advanced_newsletter_send_for) ? 'selected="selected"' : '')?>>Enabled</option>
                                    <option value="2" <?php echo (!empty($advanced_newsletter_send_for) && in_array(2, $advanced_newsletter_send_for) ? 'selected="selected"' : '')?>>Blacklist</option>
                                    <option value="3" <?php echo (!empty($advanced_newsletter_send_for) && in_array(3, $advanced_newsletter_send_for) ? 'selected="selected"' : '')?>>Un-subscribed</option>
                                    <option value="4" <?php echo (!empty($advanced_newsletter_send_for) && in_array(4, $advanced_newsletter_send_for) ? 'selected="selected"' : '')?>>Non-verfied</option>
                                </select>
							
								
								
								
                            </div>

this code convert to twig file so plessss help me…


#15
{% if advanced_newsletter_send_for and 1 in advanced_newsletter_send_for %] selected="selected"{% endif %}