在选择的foreach中比较不同的ID以显示正确的选项

时间:2019-10-28 14:50:33

标签: php html laravel input

因此,我在注册中使用html表单,在该表单中,您可以从“额外”表中看到额外选项exa_name,从额外表中看到价格,并从与额外表连接的额外选项表中进行选择。

问题: 选择(我使用foreach)显示Extraoptions-> eos_name中的所有行,并且我希望它显示eos_name的位置,其中eos_extra_id与exa_id相同!

html表单: 这就是我现在所拥有的,我不确定如何比较选项值中的eos_extra_id和exa_id

//options(exa_name from the Extra table)
<label>Options</label><br>
                        @foreach($options as $option)   
                            <div>                   
                                <input type="checkbox" class="option" id="option_{{ $option->exa_id }}" name="option_{{ $option->exa_id }}" value="{{ $option->exa_id }}" {{ isset($cache) ? (isset($cache['option_' . $option->exa_id]) ? 'checked' : '')  : (old() ? (old('option_' . $option->exa_id) ? 'checked' : '') : ($registration ? (in_array($registration ->exa_id, $registration_options) ? 'checked' : '') : '')) }} >
                                <input type="hidden" value="{{ $option->exa_price }}" class="option_price_{{ $option->exa_id }}">
                                <label>{{ $option->exa_name }}</label> <label class="exa_price">  €{{ $option->exa_price }} </label>    
                            </div>

//extra options select
<select name="extraoptions" class="form-control">
                                <option></option>
                                @foreach($extraoptions as $extraoption)
                                    <option value="{{ $extraoption->eos_id }}" 

                                        {{ (exa_id('eos_extra_id') == $extraoption->eos_id ? 'selected' : "") }}>
                                    {{ $extraoption->eos_name }}</option>
                                @endforeach
                            </select>
                       @endforeach

谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎想将$extraoptions的显示限制为具有相同exa_id的显示。为此,请在您的filter()调用中使用一个简单的@foreach()函数:

<label>Options</label><br>
@foreach($options as $option)   
<div>                   
    <input type="checkbox" class="option" id="option_{{ $option->exa_id }}" name="option_{{ $option->exa_id }}" value="{{ $option->exa_id }}" {{ isset($cache) ? (isset($cache['option_' . $option->exa_id]) ? 'checked' : '')  : (old() ? (old('option_' . $option->exa_id) ? 'checked' : '') : ($registration ? (in_array($registration ->exa_id, $registration_options) ? 'checked' : '') : '')) }} >
    <input type="hidden" value="{{ $option->exa_price }}" class="option_price_{{ $option->exa_id }}">
    <label>{{ $option->exa_name }}</label> <label class="exa_price">  €{{ $option->exa_price }} </label>    
</div>
<select name="extraoptions" class="form-control">
    <option></option>
    @foreach($extraoptions->filter(function($exOpt){ return $exOpt->eos_id == exa_id('eos_extra_id'); }) as $extraoption)
    <option value="{{ $extraoption->eos_id }}">{{ $extraoption->eos_name }}</option>
    @endforeach
</select>
@endforeach

为澄清起见,我删除了selected逻辑,因为在单个非多个selected内的多个<option>元素上设置了<select>除了将最后一个匹配选项显示为默认选择的选项外,其他功能都可以做。

相关问题