前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >循环数组

循环数组

作者头像
botkenni
发布2022-01-10 10:58:23
1.3K0
发布2022-01-10 10:58:23
举报
文章被收录于专栏:IT码农

视图:

代码语言:javascript
复制
<div id="order-item-template" class="hidden">
    <div class="item">
    <?= \yii\bootstrap\Html::activeHiddenInput($model, 'items[product_id][]', ['id' => false, 'class' => 'product_id']); ?>
    <?= \yii\bootstrap\Html::activeHiddenInput($model, 'items[product_price_id][]', ['id' => false, 'class' => 'product_price_id']); ?>
    <?= \yii\bootstrap\Html::activeHiddenInput($model, 'items[qty][]', ['id' => false, 'class' => 'qty']); ?>
    </div>
</div>

数组格式:

array ( 'product_id' => array ( 0 => '1', 1 => '16', ), 'product_price_id' => array ( 0 => '2', 1 => '', ), 'qty' => array ( 0 => '1', 1 => '1', ), )

循环:

代码语言:javascript
复制
public function validateItems()
{
    $productIdList = $this->items['product_id'];
    $qtyList = $this->items['qty'];
    $productPriceIds = $this->items['product_price_id'];

    foreach ($productIdList as $i => $product_id)
    {
        $price = null;
        $qty = $qtyList[$i];
        $product_price_id = $productPriceIds[$i];
        if(isset($product_price_id) && isset($qty) && $qty > 0)
        {
            $product = Product::findOne($product_id);
            if(null != $product && $product->isOnline())
            {
                if($product->isAreaPrice())
                {
                    if(!isset($item['product_price_id']))
                    {
                        $this->addError('items', '编号为'.$i.'的商品地区未选择。');
                    }
                    else
                    {
                        /** @var ProductPrice $pp */
                        $pp = ProductPrice::find()->where(['id' => $product_price_id, 'product_id' => $product_id])->one();
                        if(null != $pp)
                        {
                            $price = $pp->price;
                        }
                    }
                }
                else
                {
                    $price = $product->price;
                }
                $this->products[] = [
                    'product' => $product,
                    'qty' => (int)$qty,
                    'price' => $price
                ];
            }
            else
            {
                $this->addError('items', '编号为'.$i.'的商品信息不正确。');
            }
        }
        else
        {
            $this->addError('items', '编号为'.$i.'的商品信息不正确。');
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/03/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档