在编辑案例中,我通过for循环生成4个HTML div:
@for($i=0;$i < 4;$i++)
<div class="col-lg-12 no-padding">
<div class="form-group">
<select name="eng_country[]" id="eng_country" class="form-control profile-engform new-font addlisting_classic" placeholder="Country">
<option value=""> Country </option>
@foreach($result['total_exp_country'] as $key=>$value)
<option>{{$value->country_eng}}</option>
@endforeach
</select>
<span class="text-danger text_error" id="eng_country_error"></span>
</div>
</div>
@endfor
在此内容中,我填写了国家/地区的选项列表。
我在该变量结果中获得了选定的国家/地区列表,如:
array:2 [▼
0 => {#354 ▼
+"id": "1"
+"worker_id": "42"
+"country_id": "1"
+"exp_year": "2"
+"added_date": "2018-01-30 00:00:00"
+"country_eng": "India"
+"country_arb": "الهند"
+"ordering": "2"
}
1 => {#355 ▼
+"id": "2"
+"worker_id": "42"
+"country_id": "2"
+"exp_year": "1"
+"added_date": "2018-01-31 00:00:00"
+"country_eng": "Srilanka"
+"country_arb": "سيريلانكا"
+"ordering": "1"
}
]
我的问题是,我想从这个变量中选择任何值,如果它变为空白,则不应选择任何值
values:
After <?php echo "<pre>";print_r($result['total_exp_country']);exit; ?>
Array
(
[0] => stdClass Object
(
[id] => 1
[country_eng] => India
[country_arb] => الهند
[ordering] => 2
[added_date] => 2018-01-30 00:00:00
)
[1] => stdClass Object
(
[id] => 2
[country_eng] => Srilanka
[country_arb] => سيريلانكا
[ordering] => 1
[added_date] => 2018-01-31 00:00:00
)
[2] => stdClass Object
(
[id] => 3
[country_eng] => UAE
[country_arb] => الأمارات العربية المتØدة
[ordering] => 2
[added_date] => 2018-01-29 00:00:00
)
)
And array data for : $result['exp_country']
Array
(
[0] => stdClass Object
(
[id] => 1
[worker_id] => 42
[country_id] => 1
[exp_year] => 2
[added_date] => 2018-01-30 00:00:00
[country_eng] => India
[country_arb] => الهند
[ordering] => 2
)
[1] => stdClass Object
(
[id] => 2
[worker_id] => 42
[country_id] => 2
[exp_year] => 1
[added_date] => 2018-01-31 00:00:00
[country_eng] => Srilanka
[country_arb] => سيريلانكا
[ordering] => 1
)
)
I want to match id with country id for selected option ...
答案 0 :(得分:0)
请尝试以下代码:
@foreach($result['total_exp_country'] as $key=>$value)
<option
@if($result['exp_country'][$key]['country_id']==$value->country_id)
selected
@endif
value="{{$value->country_id}}">{{$value->country_eng}}
</option>
@endforeach