Solved

Issue with repeating an option in a select list

Posted on 2016-08-03
3
80 Views
Last Modified: 2016-08-03
I have a select list that is populated by the result in a query.
For example:
The result is 'Car'
so 'Car' is shown in the select menu. Thats fine.
Problem is I'd also like to show the other choices ie: Truck, SUV, Minivan etc. in case they made a mistake and need to select a different one.

So doing that is simple enough, as I loop through the possible choices after the select list is populated with the user's choice ie: 'Wagon'.

Where its a bit 'off' is because I loop through all the choices, I show 'Wagon' again in the list. Ideally I'd just want to have 'Wagon' in the list as the 'selected' choice and not repeat it.

Any idea how that is done?

Here is the image of what is going on (may help)
sample select
Code I'm using is:

<select  class="select" id="category_id" name="category_id">
@if(!isset($vehicle->category_name))
<option>Choose type...</option>
@else
<option  value="{{ $vehicle->category_name }}"> {{ $vehicle->category_name }}</option>
@endif

@foreach ($vehicle_types as $vehicle_type)
<option  value="{{ $vehicle_type->category_title }}"> {{ $vehicle_type->category_title }}</option>
@endforeach
</select>

Open in new window

0
Comment
Question by:tjyoung
3 Comments
 
LVL 15

Accepted Solution

by:
Insoftservice earned 500 total points
ID: 41740481
Not tested logic behind code is to check whether category_name matches  with category_title if yes than go selected

<select  class="select" id="category_id" name="category_id">
@if(!isset($vehicle->category_name))
<option>Choose type...</option>
@endif

@foreach ($vehicle_types as $vehicle_type)
<option  value="{{ $vehicle_type->category_title }}" @if($vehicle->category_name == $vehicle_type->category_title) selected="selected" @endif  > {{ $vehicle_type->category_title }}</option>
@endforeach
</select>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41740496
Conceptually, it seems like this might be a useful idea, but I don't know what the data structures and names look like, so I can't verify that it would be workable.  The idea is that if "wagon" is selected, then we would remove "wagon" from the array that is used to build the option tags.
<select  class="select" id="category_id" name="category_id">
@if(!isset($vehicle->category_name))
<option>Choose type...</option>
@else

<option  value="{{ $vehicle->category_name }}"> {{ $vehicle->category_name }} selected </option>
@unset($vehicle_types->$vehicle->category_name) // SOMETHING LIKE THIS, MAYBE?

@endif

@foreach ($vehicle_types as $vehicle_type)
<option  value="{{ $vehicle_type->category_title }}"> {{ $vehicle_type->category_title }}</option>
@endforeach
</select>

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41740514
Yes that did the trick. Tried both and the unset had no effect on removing the repeat as Ray suspected. Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question