Solved

Unable to populate select list with correct values from array.

Posted on 2016-08-03
8
96 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
[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
  • 5
  • 3
8 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41741813
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
ID: 41741946
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
ID: 41742549
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
Industry Leaders: 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!

 
LVL 22

Expert Comment

by:Kim Walker
ID: 41742558
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
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41742560
the pointer got me going along the right direction and way of thinking. Many thanks
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41742569
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
ID: 41742572
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
ID: 41742578
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 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…

717 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