带有 if 条件 PHP 的双 foreach
Double foreach with if condition PHP
我需要帮助,因为我在执行代码时遇到问题。
在我的数据库中,我存储了一个多 select 格式的数组:["5","6","7","8"] 我使用 json_encode 来存储我的数组。
我有一个包含 select 多个 (https://www.virtuosoft.eu/code/bootstrap-duallistbox) 的表格。
此输入填充了 table 的数据。所以这是左边的列表谁被填满了。
我想用我存储在数据库中的数据填充右边的列表。为此,我必须将 selected 放入每个对应的选项中。
但我不知道这样做......我想我使用带有 if 条件的双 foreach 但是......
一些代码来解释这一点:
Table 并保存所有选项:
Table 姓名:婚礼
字段 : id, title
Table 已保存 selected 选项
Table 姓名:步数
字段 : id, participants => 此字段有 weddings.id 的数量,例如 ["5","6","7","8"]
为了填写我的 select 列表,我这样做了
<select multiple="multiple" id="participants" class="form-control" name="participants[]">
@foreach ($weddings as $wedding)
<option value="{{ $wedding->id }}" >{{ $wedding->title }} </option>
@endforeach
</select>
为了从我这样做的步骤中检索数据
foreach(json_decode($step->participants) as $k => $v){
echo '<option value="'.$v.'" selected>'.$v.'</option>';
}
因为我需要把'selected'放在一个选项上,所以这个进入正确的列表。
如何将我的步骤 table 中的 2 个 foreach 与 "selected" 值存储与婚礼 table 结合起来?
希望你能理解我。
非常感谢。
PS :我使用 Laravel
试试这个,最重要的是理解它!
// select all participants
// for example lets say you got this from your database
$data = '["5","7","9","2"]';
// now decode it
$data = json_decode($data);
$weddings = [];
// convert values to integers
foreach($data as $key => $value)
{
$weddings[$key] = (Int) $value;
}
// create select input
$input = '<select>';
foreach($weddings as $id)
{
// lets say you want wedding id 5 to be selected
// all you have to do is add an If statement and that's it !
if($id === 5) {
// this is wedding id 5 !
$input .= '<option value="'.$id.'" selected>'.$id.'</option>';
} else if ($id === 7) {
// do something with wedding id 7 ?
$input .= '<option value="'.$id.'" class="number7">'.$id.'</option>';
} else {
$input .= '<option value="'.$id.'">'.$id.'</option>';
}
}
// close select input tag
$input .= '</select>';
echo $input;
太棒了!!!有效。
但是我需要在第二个之后更改第一个 foreach 的关闭,否则我只有循环的最后一项。
非常感谢azjezz。
有关信息,这是我的工作代码:
<div id="select-participants" class="form-group {{ $errors->has('participants') ? 'has-error' : '' }}">
<label for="participants" class="col-sm-2 control-label">{!! $errors->has('participants') ? '<i class="fa fa-times-circle-o"></i>' : '' !!}Participants</label>
<div class="col-sm-10">
<select multiple="multiple" id="participants" class="form-control" name="participants[]">
@php
$export_json_participants = json_decode($preparationStep->participants);
@endphp
@foreach($export_json_participants as $key => $value)
$export_json_participants[$key] = $value;
@foreach ($weddings as $wedding)
<option value="{{ $wedding->id }}" {{ $wedding->id == $export_json_participants[$key] ? 'selected' : '' }}>{{ $wedding->title }}</option>
@endforeach
@endforeach
</select>
@if ($errors->has('participants'))
<span class="help-block">
<strong>{{ $errors->first('participants') }}</strong>
</span>
@endif
</div>
</div>
非常感谢,没关系,我明白了。 :-)
我需要帮助,因为我在执行代码时遇到问题。 在我的数据库中,我存储了一个多 select 格式的数组:["5","6","7","8"] 我使用 json_encode 来存储我的数组。
我有一个包含 select 多个 (https://www.virtuosoft.eu/code/bootstrap-duallistbox) 的表格。
此输入填充了 table 的数据。所以这是左边的列表谁被填满了。
我想用我存储在数据库中的数据填充右边的列表。为此,我必须将 selected 放入每个对应的选项中。 但我不知道这样做......我想我使用带有 if 条件的双 foreach 但是......
一些代码来解释这一点:
Table 并保存所有选项:
Table 姓名:婚礼
字段 : id, title
Table 已保存 selected 选项
Table 姓名:步数
字段 : id, participants => 此字段有 weddings.id 的数量,例如 ["5","6","7","8"]
为了填写我的 select 列表,我这样做了
<select multiple="multiple" id="participants" class="form-control" name="participants[]">
@foreach ($weddings as $wedding)
<option value="{{ $wedding->id }}" >{{ $wedding->title }} </option>
@endforeach
</select>
为了从我这样做的步骤中检索数据
foreach(json_decode($step->participants) as $k => $v){
echo '<option value="'.$v.'" selected>'.$v.'</option>';
}
因为我需要把'selected'放在一个选项上,所以这个进入正确的列表。
如何将我的步骤 table 中的 2 个 foreach 与 "selected" 值存储与婚礼 table 结合起来?
希望你能理解我。
非常感谢。
PS :我使用 Laravel
试试这个,最重要的是理解它!
// select all participants
// for example lets say you got this from your database
$data = '["5","7","9","2"]';
// now decode it
$data = json_decode($data);
$weddings = [];
// convert values to integers
foreach($data as $key => $value)
{
$weddings[$key] = (Int) $value;
}
// create select input
$input = '<select>';
foreach($weddings as $id)
{
// lets say you want wedding id 5 to be selected
// all you have to do is add an If statement and that's it !
if($id === 5) {
// this is wedding id 5 !
$input .= '<option value="'.$id.'" selected>'.$id.'</option>';
} else if ($id === 7) {
// do something with wedding id 7 ?
$input .= '<option value="'.$id.'" class="number7">'.$id.'</option>';
} else {
$input .= '<option value="'.$id.'">'.$id.'</option>';
}
}
// close select input tag
$input .= '</select>';
echo $input;
太棒了!!!有效。
但是我需要在第二个之后更改第一个 foreach 的关闭,否则我只有循环的最后一项。
非常感谢azjezz。
有关信息,这是我的工作代码:
<div id="select-participants" class="form-group {{ $errors->has('participants') ? 'has-error' : '' }}">
<label for="participants" class="col-sm-2 control-label">{!! $errors->has('participants') ? '<i class="fa fa-times-circle-o"></i>' : '' !!}Participants</label>
<div class="col-sm-10">
<select multiple="multiple" id="participants" class="form-control" name="participants[]">
@php
$export_json_participants = json_decode($preparationStep->participants);
@endphp
@foreach($export_json_participants as $key => $value)
$export_json_participants[$key] = $value;
@foreach ($weddings as $wedding)
<option value="{{ $wedding->id }}" {{ $wedding->id == $export_json_participants[$key] ? 'selected' : '' }}>{{ $wedding->title }}</option>
@endforeach
@endforeach
</select>
@if ($errors->has('participants'))
<span class="help-block">
<strong>{{ $errors->first('participants') }}</strong>
</span>
@endif
</div>
</div>
非常感谢,没关系,我明白了。 :-)