Solved

Unable to populate select list with correct values from array.

Posted on 2016-08-03
8
45 Views
Last Modified: 2016-08-04
HI,
I am obviously very challenged with objects/arrays and am having another issue that I've tried about a thousand combinations to make work but still not working.

My goal is to populate a select list from a table column. I get very close but just can't get the syntax right no matter what I try.

Doing a var_dump of the data I'm trying to extract the values from I get this (which is expected):
array(11) { [0]=> object(stdClass)#625 (2) { ["group"]=> string(13) "payment_types" ["values"]=> string(32) "["Monthly","Bi-weekly","Weekly"]" } [1]=> object(stdClass)#626 (2) { ["group"]=> string(11) "drive_types" ["values"]=> string(77) "["Front Wheel Drive","Rear Wheel Drive","All Wheel Drive","Four Wheel Drive"]" } [2]=> object(stdClass)#627 (2) { ["group"]=> string(10) "warranties" ["values"]=> string(95) "["Remainder of Factory Warranty","Has Extended Warranty","Warranty is Available","No Warranty"]" } [3]=> object(stdClass)#628 (2) { ["group"]=> string(5) "gears" ["values"]=> string(82) "["3 Speed","4 Speed","5 Speed","6 Speed","7 Speed","8 Speed","9 Speed","10 Speed"]" } [4]=> object(stdClass)#629 (2) { ["group"]=> string(10) "fuel_types" ["values"]=> string(168) "{"Gasoline Fuel":"Gas","Diesel Fuel":"Diesel","Hybrid Fuel":"Hybrid","Flex Fuel Capability":"Flex Fuel Capable","Electric Fuel":"Electric","Propane Fuel":"Natural Gas"}" } [5]=> object(stdClass)#630 (2) { ["group"]=> string(18) "transmission_types" ["values"]=> string(40) "["Automatic","Manual","TipTronic","CVT"]" } [6]=> object(stdClass)#631 (2) { ["group"]=> string(6) "extras" ["values"]=> string(78) "["4x4","AWD","Turbo","Convertible","Cabriolet","Quattro","TDI","Supercharged"]" } [7]=> object(stdClass)#632 (2) { ["group"]=> string(9) "condition" ["values"]=> string(35) "["Excellent","Good","Fair","As Is"]" } [8]=> object(stdClass)#633 (2) { ["group"]=> string(10) "truck_cabs" ["values"]=> string(271) "["Regular Cab","Extended Cab","Crew Cab","--- Ford Specific ---","Super Cab","SuperCrew","--- Dodge Specific ---","Quad Cab","Mega Cab","--- Mazda Specific ---","Cab Plus","--- Nissan Specific ---","King Cab","--- Toyota Specific ---","Access Cab","Double Cab","CrewMax"]" } [9]=> object(stdClass)#634 (2) { ["group"]=> string(21) "vehicle_option_groups" ["values"]=> string(81) "["Comfort","Convenience","Entertainment","Safety","Exterior","Truck, Van & Jeep"]" } [10]=> object(stdClass)#635 (2) { ["group"]=> string(10) "passengers" ["values"]=> string(52) "["1","2","3","4","5","6","7","8","9","10","11","12"]" } } 

Open in new window


The particular select list I'm trying to populate is for 'Passenger Capacity' which is the last in the dump.

When I do the following in an effort to populate my select list I get this as a result:

sample pic of select list
To get it to populate like the sample pic, the code I'm using is:
@foreach ($configuration as $passengers)
<option  value="{{ $passengers->values }}" > {{ $passengers->values }}</option>
@endforeach

Open in new window


Can you see what I should be doing to drill down to the passanger capacity values and input them into the select?

Thanks as always. I very much appreciate the help.
0
Comment
Question by:tjyoung
  • 5
  • 3
8 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
Doing a var_dump of the data I'm trying to extract the values from I get this...
Is the "data you're trying to extract" in the variable $configuration? In other words, is the var_dump you posted in your question the result of
var_dump($configuration);

Open in new window

If so, $configuration contains an array of objects, each object contains two key->value pairs (group and values) and each value is a string. I believe you've already received help in converting the values string into an array.

So the first step is to iterate through the $configuration array and look for the object which has a group value of passengers
foreach ($configuration as $group) {
	if ($group->group === "passengers") {
		// convert the values string into an array and
		// iterate through this array to echo an option for each value
	}
}

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Yes thats totally correct.
But after
if ($group->group === "passengers") {
I can't see how to get the second step? I see I'm checking if the group is "passengers'.
Now that it is, how do I refer to it?

Sorry being a newbie is painful :)
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Hi, after much suffering I figured it out lol

I can output the correct variables using standard php conditional statements ie:

<select data-placeholder="Choose cab style..."  class="select" name="cab_style" id="cab_style" >
	@if(!isset($vehicle->cab_style))
		<option></option>
	@endif
	
	<?php 
		foreach ($configuration as $group) {
			if($group->group === "truck_cabs"){
				$cabs = json_decode(($group->values));
					for ($i = 0; $i < count($cabs); ++$i) {
						echo "<option  value='$cabs[$i]'> $cabs[$i]</option>";
					}
				}
			}
	?>
</select>

Open in new window


But doing the same thing the laravel blade way I get $cabs variable isn't defined. I have a few of these type of arrays so was hoping to keep to the blade method throughout.
Any idea what the difference may be? Also if you see a better 'condensed' version to get there, please let me know. Thanks very much!

<select data-placeholder="Choose cab style..."  class="select" name="cab_style" id="cab_style" >
	@if(!isset($vehicle->cab_style))
		<option></option>
	@endif

	@foreach ($configuration as $group)
		@unless($group->group === "truck_cabs")
			$cabs = json_decode(($group->values))
				@for ($i = 0; $i < count($cabs); ++$i)
				<option  value='{{ $cabs[$i] }}'> {{ $cabs[$i] }}</option>
				@endfor
		@endunless
	@endforeach
</select>

Open in new window

0
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
This is extremely complex for a "newbie." And your data isn't really conducive for this type of manipulation. Or is your data a database query result? If so, there are better ways to navigate through them. Is $configuration the result of a database query? If so, we need to change gears here -- please post the line of code that begins with or contains "$configuration=".
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
the pointer got me going along the right direction and way of thinking. Many thanks
0
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
Sorry, I am not familiar with Laravel Blade. I was not aware that they do not use PHP.
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Hi, yes the configuration is the result of a database query.
Configuration is one of the objects I'm passing back from my laravel controller. It's saved in the database that way.

Here is a screen shot of that table:
the format/data is 'adopted' from an earlier version of the site. The table are general configuration settings for various fields
table
They are used to populate various select menus etc. for a used car inventory form
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Being that these values seldom change, is there a better way I should store the values so I can step through them easier in the form layout? I also just noticed the gas values aren't saved exactly the same...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

6 Experts available now in Live!

Get 1:1 Help Now