Solved

Issue with repeating an option in a select list

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
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.

732 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