I want to display it in a loop for #foreach and display the corresponding one at the beginning
<label for="user_name">employee</label>
<select name="user_name">
<option value="{!!null!!}" #if($param['user_name'] == '') selected #endif>no</option>
#foreach($users as $user)
<option value="{{$user->name}}" #if($param['user_name'] == '{{$user->name}}') selected #endif>{{$user->name}}</option>
#endforeach
</select>
However, this is not a good choice
Is there no good way
There is no need to use {{}} or echo the variable inside of #if directive like we donot compare the value in simple PHP by echo the variable.
So #if can be written simply as :
#if($param['user_name'] == $user->name)
and you need to re-write second option like inside the foreach like:
<option value="{{$user->name}}" #if($param['user_name'] == $user->name) selected #endif>{{$user->name}}</option>
Hope it works for you now.
Related
I have a problem with passing the selected index to my controller via click.
If I manually change the index in the browser, it is working.
(http://localhost:3000/admin/users?user=&sortBy=5)
$sortOptions is the name of my 2d array in my controller.
sortDisplay is a field in my 2d array in my controller.
Am I missing something in my foreach loop?
<label for="sortBy">Sort by</label>
<select class="form-control" name="sortBy" id="sortBy">
#foreach($sortOptions as $index => $sortOptions)
<option value="{{$index}}" {{ (request()->sortBy == $index ? 'selected' : '') }}>
{{$sortOptions["sortDisplay"]}}
</option>
#endforeach
</select>
Use jQuery to submit form when value changed
So your page will refresh and you will get what you want
In the foreach loop, you are assigning the same variable name as the variable you are iterating. In your case, after the first loop, you re-instantiate the $sortOptions variable with the content of the first index of $sortOptions.
#foreach($sortOptions as $index => $sortOption) // <-- $sortOption, not $sortOption(s)
<option value="{{$index}}" {{ (request()->sortBy == $index ? 'selected' : '') }}>
{{ $sortOption["sortDisplay"] }}
</option>
#endforeach
I am trying to use 'old' in a Laravel 5 app to pre-populate a select form on an edit route like this...
<select id="category" name="category" required>
<option value="">Select</option>
<option value="animal" #if (old('category') == "animal") {{ 'selected' }} #endif>Animal</option>
<option value="vegetable" #if (old('category') == "vegetable") {{ 'selected' }} #endif>Vegetable</option>
<option value="mineral" #if (old('category') == "mineral") {{ 'selected' }} #endif>Mineral</option>
</select>
This works well and keeps the selected option if a validation fails, but I am trying to make it work so that it pre-populates when the page first loads.
How do I determine if this is the first load of the edit page, or if it has reloaded after a validation failure? Or is there a better way to do this?
Lets imagine you have sent the category value as $category.
So in every <option> tag,
<option value="animal"
{{ old('category') == 'animal' ? ' selected' :
$category == 'anumal' ? ' selected' : '' }}>
Animal
</option>
This is what is going on there:
if there is an old value and its matching, select this,
else if the original value is matching select this,
else do nothing.
Use the second parameter of the old function with a default/initial value:
<option value="animal" #if (old('category', 'animal') == "animal") {{ 'selected' }} #endif>Animal</option>
The second parameter will be returned as the function result if an old value cannot be found in the flashed input.
I want to show selected value from database into dropdown list on page load.
My controller index function is :
public function index()
{
$country_data =DB::table('country')->select('country_id','country_name')->get();
$profile_data= DB::table('profiles')->select('*')->where('id',$user_id)->first();
return view('profile_update',compact('profile_data','country_data'));
}
Column name in database for height is :Height
My dropdown in profile_update.blade.php is
<select class="select4" name="country" id="country">
<option value="">Please Select</option>
#foreach($country_data as $country)
<option value="{{$country->country_id}}" {{$country_data->country == $country->country_id ? 'selected' : ''}}>{{$country->country_name}}</option>
#endforeach</select>
This is a example of how I do this:
<select class="js-states browser-default select2" name="shopping_id" required id="shopping_id">
<option value="option_select" disabled selected>Shoppings</option>
#foreach($shoppings as $shopping)
<option value="{{ $shopping->id }}" {{$company->shopping_id == $shopping->id ? 'selected' : ''}}>{{ $shopping->fantasyname}}</option>
#endforeach
</select>
In order to understand it fully you will need basics of laravel (MVC),
Let suppose, you have controller. In my case I made a separate table for drop down values.
My Approach --- Separate table for values of drop down, you can try different approach but my explanation was mainly focused on concept.
Note: PersonInfo is model, sampleType is model of my drop down values and don't forget to make a route for the controller.
ExampleController{
public funtion getValues($id){
/*
I am fetching information of a single row from the database by id and
then passing it to the view. The $id to the function came from the
request by your view/form.
I also fetched the values of drop down from the separate table
of database and passed it to the exampleView.
*/
$selectedValue=PersonInfo::findOrFail($id);
$sampleType=SampletypePicker::all(); //model for fetching all values of drop down
return view('exampleView',compact('selectedValue','sampleType));
}
}
So, in the above controller I fetched all values and passed it to the ExampleView. Now in your View file you have to work likewise.
exampleView.blade.php add the below code
<?php $options=$selectedValue->sample_type ?> //note take the value from the database which stored for the individual record and match it with the selected one.
<select class="form-control" name="test">
<option>Select Test Type</option>
#foreach ($sampleType as $value)
<option value="{{ $value->sample_type }}" {{ ( $value->sample_type == $options) ? 'selected' : '' }}>
{{ $value->sample_type }}
</option>
#endforeach
</select>
It is one of the best approach when you want to add dynamic values to the drop down. I mean if you want to add more values to the select tag options then this is the best approach.
Another Approach - take the idea
<?php $months = array("Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); ?>
<?php $options=$patient_data->month ?>
#if($patient_data->month)
<select id="expiry_month" name="month" class="form-control-sm">
#foreach($months as $month)
<option value="{{$month}}" {{($month==$options)? 'selected':'' }}>{{$month}}</option>
#endforeach
</select>
#endif
<span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
</div>
OUTPUT of ABOVE CODE
#Sarita Sharma show the error(s). Maybe then anyone help you to resolve this problem. Show data in colections in controler - use dd e.g.
dd($country_data);
dd($profile_data);
Do you want to display the country in the view with the appropriate Height value from profile_data?
Ps. How to put the code, please use the formatting code - put the code in `` and use camelCase in value name (maintaining good PSR-1 practice) - it is easier to read code.
Well, simply if anyone still looking for this, here is the simple way to select items for dropdown from database to blade view
$users = Users::pluck('name', 'id');
$selectedID = 2;
return view('users.edit', compact('id', 'users'));
add this in the controller function before rendering the view
and in the blade view, simply use this variable as foreach loop as
<select name="user_id" class="form-control" id="exampleFormControlSelect1">
#foreach ($users as $key => $value)
<option value="{{ $key }}" {{ ($key == $selectedID) ? 'selected' : '' }}>
{{ $value }}
</option>
#endforeach
</select>
if you don't want to select all users from DB, you can use where condition before pluck condition as
$users = Users::where('status', 1)->pluck('name', 'id');
works with Laravel 8 and Laravel6
I have Blade view with some dropdowns. Also, I am passing data to this view through my controller.
<select name="location">
<option value="some-value1">Some Label1</option>
<option value="some-value2">Some Label2</option>
</select>
and I have property $model->location. How do I append selected to the right option? Is it even possible to do that?
You can use a ternary operator in a Blade slot, for example:
<option value="value" {{ $model->location == 'value' ? 'selected' : '' }}>Label</option>
The selected option will only be displayed when your condition ($model->location == 'value') is true
how to disable option in drop down box using code igniter?i want to disable the value"------"in this drop down
echo "<tr class='background1'><td class='checkoutfield'>";
$countryall='';
$select='';
if(isset($order)) $country=$order['varShippingCountry']; else $country='';
if(isset($countries) && $countries !='') {
$countryall['']="Select";
foreach($countries as $key=>$value):
$countryall["226/United States"]="United States";
if($value['id'] !='226') {
//<option value=”spider” disabled=”disabled”>Spider</option>
$countryall['0']="-------------------------------------------------------";
$countryall["$value[id]/$value[varPrintableName]"]=$value['varPrintableName'];
}
if($value['id'] == $country)
$select="$value[id]/$value[varPrintableName]";
endforeach;
}
$selFunc='style="width:190px;" id="varShippingCountry" class="required" onchange="stateajax(this.value)" onKeyup="return changeText(\'varShippingCountry\',\'varPaymentCountry\',\'this.value\')"';
echo form_label('Country','varShippingCountry')."<span class='mandatory'>*</span></td><td>";
echo form_dropdown('varShippingCountry',$countryall,$selFunc);
You can't disable an item in a HTML select element - only disable the entire element.
Are you trying to make a separator between different sections of the list? In that case, you can use <optgroup>.
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
You can make this happen in CodeIgniter by passing it a multidimensional array.
Alternatively, if you are just trying to make the first and default item look like ------, then add it in the normal way with an empty value. Then on validation, check that an empty value has not been passed.