set default selected db value in select element php laravel - php

<td>
<select name="setR" class="form-control">
<option value="">--Select--</option>
#if($ap_detail['pr'])
<option selected="selected" value="{{$ap_detail['pr']->setR}}">
#endif
#for($i=1; $i<=31; $i++)
{{$ap_detail['pr']->setR}} Days Later</option>
<option value="{{$i}}">{{$i}} Days Later</option>
#endfor
</select>
</td>
if value exist in databse it select by default that value
I tried above code its working fine but it select the db selected value but also show in looping list I mean if 4 comes from database it shows two 4 values it should be one 4 value

I think this might help you. So in loop if your database value occurs then add selected="selected" to that option instead of printing that option differently.
<td>
<select name="setR" class="form-control">
<option value="">--Select--</option>
#for($i=1; $i<=31; $i++)
#if($ap_detail['pr'] && $ap_detail['pr']->setR == $i)
<option selected="selected" value="{{$ap_detail['pr']->setR}}">{{$ap_detail['pr']->setR}} Days Later</option>
#else
<option value="{{$i}}">{{$i}} Days Later</option>
#endif
#endfor
</select>
</td>

Related

How to set default option for <select> based on PHP variable (POST / GET / SESSION etc.)

I am using HTML + PHP to build a web App.
I wanted to create an editable form that includes a couple of <select> fields. As an example, we can assume that I want to create a user profile.
The thing is, when new user wants to edit some data about himself, all fields are empty. For the field sex it may look like this:
<select class="form-select" name="sex" required>
<option value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
or:
<select class="form-select" name="sex" required>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
Whereby default the empty value described by "-" will be displayed.
But what about when the user already has filled that field? Let's assume we have an object variable $user and may access his/hers sex by $user->sex. By default, I want the <select> to be selected with the proper attribute. Using PHP I may go for something like this:
<select class="form-select" name="sex" required>
<?php if ($user->sex == 'M') : ?>
<option value="">-</option>
<option selected value="M">Male</option>
<option value="F">Female</option>
<?php elseif($user->sex == 'F' : ?>
<option value="">-</option>
<option value="M">Male</option>
<option selected value="F">Female</option>
<?php else : ?>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
<?php endif; ?>
</select>
But that requires a lot of if, elseif statements. As an example, I've provided only three-optional field. What about when there are more options?
Is there any elegant solution to choose a selected option of <select>, based on a variable (object fetched from database / POST / GET / SESSION - does not really matter), without using so many ifs? At best, I'd like to list possible options only once and then tell the site which is to be chosen by default.
Thanks everyone in advance!
Why do you repeat the options? Just check every option if it needs to be selected...
<select class="form-select" name="sex" required>
<option <?php if($user->sex == "") echo "selected" ?> value="">-</option>
<option <?php if($user->sex == "M") echo "selected" ?> value="M">Male</option>
<option <?php if($user->sex == "F") echo "selected" ?> value="F">Female</option>
</select>

How to get id of selected value from dropdown and use it in other dropdown in laravel?

I'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.

How to fetch value from objects in laravel?

I m making an app in which I want to get value of an object and also I want to get id while creating new user.
I was using drop down menu, and want to get id which was selected in previous dropdown.
#php
use Illuminate\Support\Facades\DB;
$uid=DB::table('users')->where('role_id','4')->get();
$counter=0;
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',array('class_id' => $cid->id('class_id')))->get();
$counterrr=0;
#endphp
<select user="" name="parent_id">
<option value="null">User</option>
#foreach($uid as $user)
#if($counter==0)
<option selected="selected" value="{{$user->id}}">{{$user->name}}</option>
{{$counter++}}
#else
<option value="{{$user->id}}">{{$user->name}}</option>
#endif
#endforeach
</select>
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
As now I want that the object shows the value of selected id in class-dropdown menu and I want to put it in selected list of section
$sec=DB::table('sections')->where('class_id',$cid)->get();
I' m having issues in writing syntax over here.('class_id',$cid)
Also I want that a id on which this new user is going to be created , how to get that?

How to populate old select option values?

I have a select box in my form that looks like below :
<select class="form-control" name="category_id" id="category_id">
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}">{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif
</select>
My question is, how do I populate the select option with old selected option when the validation fails ? By populate I mean like {{old('input')}}..
I tried this way :
#if(old('category_id')
<option value="{{old('category_id')}}">{{old name ???}}</option>
#endif
But it will only get the category_id value, not the category name. How to get the old category name as well ? that's all and thanks!
you can do like this
#if(count($category_list)>0)
<option value="null">Pick a Category</option>
#foreach($category_list as $cl)
<option value="{{$cl->id}}" #if(old('category_id') == $cl->id) selected="selected" #endif>{{$cl->name}}</option>
#endforeach
#else
<option>No categories found</option>
#endif

How to limit drop down menu within a time interval using Angular.js and PHP

I need one help.I need to active one drop down menu for 2 days then after 2 days it will not display to the user using PHP and Angular.js. I am explaining my code below.
<tr ng-repeat="p in listOfProductData">
<td>
<select class="form-control" id="status" ng-model="order_status" ng-change="changeOrderStatus(order_id,shipping_email,p.pro_data_id,order_status)">
<option value="">Select Status</option>
<option value="Ordered">ORDERED</option>
<option value="InProgress">IN-PROGRESS</option>
<option value="Shipped">SHIPPED</option>
<option value="Delivered">DELIVERED</option>
<option value="Canceled">CANCELED</option>
<option value="Return">RETURN</option>
</select>
</td>
</tr>
In the above select menu i have one RETURN option i need to active it for 2 days according to the datetime saved in my database.I am explaining my table below.
db_time:
id cur_time
1 2016-03-16 14:33:19
at each page reload it will check the this time in database if it 2 days is already gone the return option will not display to user.listOfProductData will contain the value from db_time table.Please help me.
After check db_time use data-ng-show to show and hide menu for users.
try this
<?php
$db_date = "2016-03-16 14:33:19";
$curr_date=date_create(date("Y-m-d"));
$diff=date_diff($curr_date, $db_date);
$days = $diff->format("%a");
if($days <= 2) {?>
<select class="form-control" id="status" ng-model="order_status" ng-change="changeOrderStatus(order_id,shipping_email,p.pro_data_id,order_status)">
<option value="">Select Status</option>
<option value="Ordered">ORDERED</option>
<option value="InProgress">IN-PROGRESS</option>
<option value="Shipped">SHIPPED</option>
<option value="Delivered">DELIVERED</option>
<option value="Canceled">CANCELED</option>
<option value="Return">RETURN</option>
</select>
<?php
}
?>

Categories