选中php动态复选框

时间:2016-02-18 06:02:02

标签: 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的新手,所以无法理解如何做到这一点??

1 个答案:

答案 0 :(得分: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