带有分页的Yii2会话数组

时间:2016-12-18 11:02:49

标签: jquery arrays session pagination yii2

你好,我有使用分页存储会话数组的问题。我做网上商店,我有商店页面,我有一些卖东西,在“购买”按钮我为数据库中的每个项目设置ID。而且我需要制作1个可以分页的数组,这样我就可以在“购物车”中使用这些ID,这样用户就可以查看他实际购买的内容。以下是我对销售项目的看法:

<?php foreach ($stock as $stocklist): ?>
    <div class="col-sm-4"> 
        <div class="prod_box">
            <div class="product_title"  value="<?php $stocklist->name ?>" >  <?php echo $stocklist->name ?> </div>
            <div class="prod_price"  value="<?php $stocklist->price ?>">   <?php echo $stocklist->price ?>  $ </div>
            <div class="prod_buy">
                <button type="submit" value="<?= $stocklist->id ?>" class="custombtn" method='post'  title="Add to Cart" > Buy </button>
            </div>
        </div>
    </div>
<?php endforeach; ?>

我还使用JS(Ajax)将每个购买的项目发送给控制器,并将每个ID放入数组:

<script type="text/javascript">
    $(document).ready(function () {
        var IDs = [];
        $('.custombtn').click(function () {
            var id = $(this).attr("value");
            IDs.push(id);
            $.ajax({
                type: 'POST',
                url: '<?php echo Url::to(['stock/shop']); ?>',
                data: {IDs},
                dataType: 'json'
            });
            console.log(IDs);
        });
    });
</script>

在控制器中我有:

public function actionShop() {

    Yii::$app->session->open();
    $session = Yii::$app->session;

    $query = Stock::find();
    $pagination = new Pagination([
        'defaultPageSize' => 6,
        'totalCount' => $query->count(),
    ]);
    $stock = $query->orderBy('id')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

    if (Yii::$app->request->post()) {

        $session = Yii::$app->session;

        $data = Yii::$app->request->post();

        $session['lastIDs'] = $data;
    }

    return $this->render('shop', [
                'stock' => $stock,
                'pagination' => $pagination,
    ]);
}

所以:

$session['lastIDs'] = $data;

我保存了会话中的所有ID,但是当我用分页切换页面并点击更多东西来购买时,项目ID被覆盖。我还尝试制作2个会话变量并合并数组,但随后它保存了所有的API调用,因此我购买了20个“20”项目的数组,每个数组都有+1 ID作为最后一个点击,不仅仅是1个数组全部已购买的ID。

3 个答案:

答案 0 :(得分:1)

使用:

$(document).ready(function(){

var IDs = [];
$('.custombtn').click(function() {
    var id = $(this).attr("value");
    IDs.push(id);
    console.log(IDs);
    $.ajax({
        type: 'POST',
        url: '<?php echo Url::to(['stock/shop']); ?>',
        data: {
            data: IDs
        },
        dataType: 'json'
    });
});

});

答案 1 :(得分:0)

然后尝试这个

public function actionShop(){

if (!Yii::$app->session->isActive) {

    Yii::$app->session->open();

    $query = Stock::find();
    $pagination = new Pagination([
        'defaultPageSize' => 6,
        'totalCount' => $query->count(),
    ]);
    $stock = $query->orderBy('id')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();


}
return $this->render('shop', [
            'stock' => $stock,
            'pagination' => $pagination,
]);

}

答案 2 :(得分:0)

JS中的ReWritten:

<script type="text/javascript">
    $(document).ready(function () {
        $('.custombtn').click(function () {
            var id = $(this).attr("value");
            console.log(id);
            $.ajax({
                type: 'POST',
                url: '<?php echo Url::to(['stock/ajax']); ?>',
                data: {id: id},
                dataType: 'json'
            });
        });
    });
</script>

将actionAjax设为:

public function actionAjax() {

    if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
        Yii::$app->session->open();
        $session = Yii::$app->session;
        if (!isset($session['selectedItems'])) {
            $session['selectedItems'] = [];
        }

        $allItemsIDs = $session['selectedItems'];
        $itemID = Yii::$app->request->post("id");
        $allItemsIDs[] = $itemID;
        $session['selectedItems'] = array_unique($allItemsIDs);

        return "OK";
    }
}

和actionShop仍然存在:

 public function actionShop() {


    $query = Stock::find();
    $pagination = new Pagination([
        'defaultPageSize' => 6,
        'totalCount' => $query->count(),
    ]);
    $stock = $query->orderBy('id')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

    return $this->render('shop', [
                'stock' => $stock,
                'pagination' => $pagination,
    ]);
}