2016-02-18 56 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其他复选框