2016-07-19 94 views
1

我想比较下面的选择字段,以防止用户提交相同的水果名称。防止重复值在选择字段

例如

Field #1: Apple 
Field #2: Apple 
Field #3: Banana 

的代码,我到目前为止有:

预先浏览

<table class="table table-bordered"> 
     <thead> 
      <th>Product</th> 
      <th>Quantity</th> 
     </thead> 
     <tbody class="body"> 
     <tr> 
      <td>{!! Form::select('fruits[]', $products, null, ['class'=>'form-control']) !!}</td> 
      <td>{!! Form::text('quantity[]', null, ['placeholder' => 'Insert Quantity', 'class' => 'form-control']) !!}</td> 
     </tr> 
     <tr> 
      <td>{!! Form::select('fruits[]', $products, null, ['class'=>'form-control']) !!}</td> 
      <td>{!! Form::text('quantity[]', null, ['placeholder' => 'Insert Quantity', 'class' => 'form-control']) !!}</td> 
     </tr> 
     <tr> 
      <td>{!! Form::select('fruits[]', $products, null, ['class'=>'form-control']) !!}</td> 
      <td>{!! Form::text('quantity[]', null, ['placeholder' => 'Insert Quantity', 'class' => 'form-control']) !!}</td> 
     </tr> 
     </tbody> 
    </table> 

控制器

$products = ['Apple', 'Banana', 'Orange']; 

$inputs = Input::all(); 

for ($i = 0; $i < count($inputs['fruits']); $i++) { 

    if($inputs['fruits'][$i] == ?????){ <-- Here is my problem 

    return back(); 

    } 
} 

感谢您的帮助

回答

0

如何存储您选择的产品的临时变量?

$products = ['Apple', 'Banana', 'Orange']; 

$inputs = Input::all(); 

$selected_products = []; 
for ($i = 0; $i < count($inputs['fruits']); $i++) { 
    $product = $inputs['fruits'][$i]; 

    if (isset($selected_products[$product])) 
     return back(); 

    $selected_products[$product] = true; 
} 
+0

Thanks @MarcoFlorian!你节省了我的时间:) – Khai