dongnuo4594 2018-10-31 04:31
浏览 68

Yii2商店产品ID用逗号分隔

I want to add proid in $model->product which is in different table I want to get product id that are added into cart and store them in the DB(phpmyadmin) seperated by comma

controller.php(where i get session from)

class CartController extends \yii\web\Controller
{
    public $totalItems=0;
    public $totalPrice=0.00;
    //public $netPrice = 0;

    public function actionAdd($id = null)
    {
        if(!intval($id) || empty($id)){
           Yii::$app->session->setFlash('error','cannot find this product');
            return $this->redirect('/front');

        }

        if(!isset(Yii::$app->session['cart'])){
            Yii::$app->session['cart'] = [];
            Yii::$app->session['total_items'] = 0;
            Yii::$app->session['total_price'] = 0.00;

        }
           $this->addtocart($id);

           $this->setTotal();
           //$model = new Orders();
           //$model->ordername = ;
              // $model->ordertotal = $netPrice ;
            return $this->redirect('index');


    }
    public function addtocart($id){
        if(isset(Yii::$app->session['cart'][$id])){
            $session = Yii::$app->session['cart'];
            $session[$id]= $session[$id] +=1;
            Yii::$app->session['cart']= $session;
        }
        else{
            $session = Yii::$app->session['cart'];
            $session[$id] = 1;
            Yii::$app->session['cart'] = $session;
        }
    }
    public function setTotal(){
        Yii::$app->session['total_items']=$this->totalItems(Yii::$app->session['cart']);
        Yii::$app->session['total_price']=$this->totalPrice(Yii::$app->session['cart']);

        $this->totalItems = Yii::$app->session['total_items'];
        $this->totalPrice = Yii::$app->session['total_price'];





    }
    public function totalItems($cart){
        $totalItems = 0;
        if(is_array($cart)){
            foreach ($cart as $id=>$qty){
                $totalItems += $qty;
            }

            return $totalItems;
        }
        //return $totalItems;
    }
    public function totalPrice($cart){
        $netPrice = 0.00;
        if(is_array($cart)){
            foreach ($cart as $id=>$qty){
                $item = $this->findProduct($id);
                $netPrice += $item->price * $qty;
            }
            //return $netPrice;
        }
        return $netPrice;
    }


    public function findProduct($id){
        return Products::findOne($id);
    }
    public function updateCart(){
        foreach(Yii::$app->session['cart'] as $id=>$qty){
            if(isset($_POST[$id])){
                if($_POST == 0){
                    $session = Yii::$app->session['cart'];
                    unset($session[$id]);
                    Yii::$app->session['cart'] = $session;
                }
                else{
                    $cart = Yii::$app->session['cart'];
                    $cart[$id] = $_POST[$id];
                    Yii::$app->session['cart'] = $cart;
                }
            }
        }
    }


    public function actionIndex()
    {
        if(!isset(Yii::$app->session['cart']) || empty(Yii::$app->session['cart'])){
            Yii::$app->session->setFlash('error','cart is empty');

        }
// i made a table named order with colums ordertotal and products where ordertotal should store the total value and products should store the product id which are added to cart got the order total but didn't get products
            $model = new Orders();
            $model->ordertotal = $this->totalPrice(Yii::$app->session['cart']); //got the order total from this 
           //$model->product=add the product id( <?= Html::a('AddToCart',['/cart/add','id'=> $p->proid], ['class' => 'btn btn-success']) ?> //this is my product page where i 'add to cart' from) seperated by comma

        if($model->save()){
            print_r('ok');

        }
        else{
           echo $model->getErrors();

        }



        return $this->render('index',[
            'totalItems' => $this->totalItems(Yii::$app->session['cart']),
            'totalPrice' => $this->totalPrice(Yii::$app->session['cart']),
        ]);
    }

}

i got the above code from t=PLX5MZfWdby5QFDLU-Ov7Ixv6AilU-mgcQ"

implementing new model and adding it to db is my implemtation

product.php

<?php foreach ($product as $p) { ?>
                <div class="col-md-4">

                    <h2><?php echo $p->name?></h2>
                    <?php echo "<br/>"?>
                    <?php echo "Name: ".$p->name?>
                    <?php echo "<br/>"?>


                     <?php echo "price: ".$p->price?>
                    <?php echo "<br/>"?>
                    <?= Html::a('AddToCart',['/cart/add','id'=> $p->proid], ['class' => 'btn btn-success']) ?>



                </div>
            <?php } ?>
  • 写回答

1条回答 默认 最新

  • duanqin7791 2018-10-31 05:38
    关注

    You must alter serveral functions, first need to alter addtocart function.

    adding Yii::$app->session['cart']['id'][] = $id;

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端