Solved

Issue with repeating an option in a select list

Posted on 2016-08-03
3
92 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

735 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