[英]PHP/ Laravel: How to Give An If Statement a Dynamic Condition?
在我的報價模塊中,產品可以分為各種不同的類別。 類別也可以創建和刪除。 下面的代碼工作正常,但這僅是因為我已經對類別進行了硬編碼。 您將看到,如果category_id = 1,則顯示屬於“汽車”的產品。 如果它是“ 2”,則顯示“摩托車”。
這是完全不靈活的。 我希望能夠動態創建一個新類別,並在表單上顯示該新類別。 那么,如何做才能允許動態創建或刪除類別?
我正在使用Laravel,但是,PHP回答同樣好。
該代碼是Laravel視圖,但我認為這很明顯是怎么回事:
@foreach($quotation as $quote)
@if($quote->category_id == 1)
<tr>
<td>{!! Form::select('product_id[0]',$products['cars'], $quote->name, ['class'=>'product_id']) !!}</td>
<td>{!!Form::text('quantity',$quote->quantity) !!}</td>
<td id="price">{{$quote->price}}</td>
<td id="cost">{{$quote->cost}}</td>
</tr>
@endif
@if($quote->category_id == 4)
<tr>
<td>{!! Form::select('product_id[0]',$products['motorcycle'], $quote->name, ['class'=>'product_id']) !!}</td>
<td>{!!Form::text('quantity',$quote->quantity) !!}</td>
<td id="price">{{$quote->price}}</td>
<td id="cost">{{$quote->cost}}</td>
</tr>
@endif
@endforeach
控制器:
public function quote_edit($id)
{
$customer = $this->quotation->get_customer($id); //array
$products = $this->quotation->get_products(); //array of collections
$quotation = $this->quotation->get_quote($id); //array
$count = count($quotation);
return View('quotations/edit_quote_test2', compact('count','quotation', 'products', 'customer') ) ;
}
模型:
public function get_quote($id){
return DB::table('quotations')
->join('products', 'products.product_id', '=', 'quotations.product_id')
->select( 'products.name','products.category_id','products.price','quotations.product_id','quotations.quantity','quotations.cost','quotations.reference')
->where('quotations.reference', '=', $id)
->get();
}
這是$ products的print_r()結果的示例
{id: "7",
name: "Stanley Hook Blades 11-939",
product_id: "3463",
description: null,
long_description: null,
price: "62.00",
category_id: "7",
color: null,
coverage: null,
barcode: null,
qrcode: null,
box_quantity: null,
supplier_id: "1",
location_id: null,
alternate_id: null,
created_at: "0000-00-00 00:00:00",
updated_at: "0000-00-00 00:00:00",
category_name: "tools"
}
這是用於報價的print_r()結果的示例:
{
name: "asphalt",
category_id: "9",
category_name: "tear_install",
price: "300.00",
product_id: "3453",
quantity: "1",
cost: "300.00",
reference: "jac310316-75",
total: "15900.00"
}
您可以使用category_id
加入category table
。 我假設您具有表名稱類別,並且具有自動遞增列category_id和category_name列作為名稱,因此您可以在select語句中使用該列。
模型:
public function get_quote($id){
return DB::table('quotations')
->join('products', 'products.product_id', '=', 'quotations.product_id')
->leftJoin('category', 'category.category_id', '=', 'product.category_id')
->select( 'products.name','products.category_id', 'category.category_name','products.price','quotations.product_id','quotations.quantity','quotations.cost','quotations.reference')
->where('quotations.reference', '=', $id)
->get();
}
視圖:
@foreach($quotation as $quote)
<tr>
<td>{!! Form::select('product_id[0]',$products[$quote->category_name], $quote->name, ['class'=>'product_id']) !!}</td>
<td>{!!Form::text('quantity',$quote->quantity) !!}</td>
<td id="price">{{$quote->price}}</td>
<td id="cost">{{$quote->cost}}</td>
</tr>
@endforeach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.