Solved

Issue with repeating an option in a select list

Posted on 2016-08-03
3
52 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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now