2016-02-18 57 views
0

這是我所有的運輸方式:PHP動態複選框被選中

$Shipping = Shipping::find()->all();  

$allshp_mthd = []; 
    foreach ($Shipping as $P_Shipping) { 
     $allshp_mthd[$P_Shipping->id_shipping]['id_shipping'] = $P_Shipping->id_shipping; 
     $allshp_mthd[$P_Shipping->id_shipping]['shipping_method'] = $P_Shipping->shipping_method; 
    } 

輸出:

Array 
(
[1] => Array 
    (
     [id_shipping] => 1 
     [shipping_method] => free 
    ) 

[2] => Array 
    (
     [id_shipping] => 2 
     [shipping_method] => pickup 
    ) 

[3] => Array 
    (
     [id_shipping] => 3 
     [shipping_method] => country_wise 
    ) 

[4] => Array 
    (
     [id_shipping] => 4 
     [shipping_method] => api 
    ) 

) 

這是保存在數據庫中我的送貨方式:

$ProductShipping = ProductShipping::find()->where(['id_product' => $id_product,'id_shop' => $current_shop])->all(); 

$shiping_name = []; 
    foreach ($ProductShipping as $Product_Shipping) { 
     $Shpng = Shipping::find()->where(['id_shipping'=>$Product_Shipping->id_shipping])->one();   
     $shiping_name[$Product_Shipping->id_shipping]['id_shipping'] = $Product_Shipping->id_shipping; 
     $shiping_name[$Product_Shipping->id_shipping]['shipping_method'] = $Shpng->shipping_method; 
    } 

輸出:

Array 
(
[4] => Array 
    (
     [id_shipping] => 4 
     [shipping_method] => api 
    ) 

[2] => Array 
    (
     [id_shipping] => 2 
     [shipping_method] => pickup 
    ) 

) 

在php中,我希望所有的複選框都將在那裏與API,皮卡檢查在視圖中。 這怎麼可能? 我是新的PHP所以不能理解如何做到這一點?

+0

顯示你的HTML你已經嘗試了什麼? – Vinie

+0

我還沒有創建我的HTML到現在。 –

+0

然後使用foreach和檢查條件,然後添加檢查您的複選框,如果發現值 – Vinie

回答

0

我認爲$output包含您的運輸詳細的輸出你的問題。我假設你有一個複選框的每個類別在你的HTML。

//for picked check box 
foreach($output as $ship) 
{ 
    if($ship['id_shipping'] == 2 and $ship['shipping_method'] == 'pickup') 
    { 
      $checked = 'Checked="Checked"'; 
      break; 
    } 
} 
<input type="checkbox" <?php echo $checked;?> /> 

做simmilar其他複選框