Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Issue with repeating an option in a select list

Posted on 2016-08-03
3
Medium Priority
?
133 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 2000 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 111

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 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