Avatar of tjyoung
tjyoung

asked on 

Unable to populate select list with correct values from array.

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:

User generated image
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.
LaravelPHP

Avatar of undefined
Last Comment
tjyoung
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of tjyoung
tjyoung

ASKER

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 :)
Avatar of tjyoung
tjyoung

ASKER

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

Avatar of Kim Walker
Kim Walker
Flag of United States of America image

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=".
Avatar of tjyoung
tjyoung

ASKER

the pointer got me going along the right direction and way of thinking. Many thanks
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Sorry, I am not familiar with Laravel Blade. I was not aware that they do not use PHP.
Avatar of tjyoung
tjyoung

ASKER

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
User generated image
They are used to populate various select menus etc. for a used car inventory form
Avatar of tjyoung
tjyoung

ASKER

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...
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo