购物车cakephp + bootstrap莫代尔

时间:2015-11-16 03:15:20

标签: cakephp shopping-cart bootstrap-modal

我的购物车系统使用cakephp + bootstrap模式有问题。问题是,当我单击/选择我的一个图像时,它将添加到购物车,但它将始终显示我的数据库中的最后一项/数据。即使我选择了第一个项目,它仍会显示我数据库中的最后一项/数据。请帮我解决这个问题。

CartsController.php

class CartsController extends AppController {

public $uses = array('Sidedish','Cart');

public function add() {
    $this->autoRender = false;
    if ($this->request->is('post')) {
        $this->Cart->addProduct($this->request->data['Cart']['product_id']);
    }
    echo $this->Cart->getCount();
}

public function view() {
    $carts = $this->Cart->readProduct();
    $side_dishes = array();
    if (null!=$carts) {
        foreach ($carts as $productId => $count) {
            $side_dish = $this->Sidedish->read(null,$productId);
            $side_dish['Sidedish']['count'] = $count;
            $side_dishes[]=$side_dish;
        }
    } 
    $this->set(compact('side_dishes'));
            print_r($carts);
            print_r($side_dishes);
}}

views / Orders.ctp - >这是我需要点击要在购物车中显示的项目的地方。

                  <div class="col-sm-12">
                  <?php echo $this->Form->create('Cart',array('id'=>'add-form','url'=>array('controller'=>'carts','action'=>'add')));?>
                  <ul class="list-inline">
                      <?php foreach ($side_dish as $sidedish):?>
                      <li>
                        <?php echo $this->Form->hidden('product_id',array('value'=>$sidedish['Sidedish']['sidedish_id'])); ?>
                        <?php 
                        echo $this->Form->submit(//$sidedish['Sidedish']['sidedish_id'],
                                '',array(
                'name'=>'submit',
                                    'style'=>'height:130px;width:200px;'
                                    . 'background-repeat:no-repeat;'
                                    . 'border:none;'
                                    . 'background-image:url(/webapp' .$sidedish['Sidedish']['sidedish_img']. ')'));
                        ?>
                        <h5 class="text-center"><?php echo $sidedish['Sidedish']['sidedish_name'];?></h5>
                        <h5 class="text-center">$<?php echo $sidedish['Sidedish']['sidedish_price'];?></h5>
                      </li>
                      <?php endforeach;?>
                  </ul>
                  <?php echo $this->Form->end();?>

              </div>

有些人告诉我问题是我的views / orders.ctp中的foreach循环,有些人告诉我问题出在控制器中。我不确定这里的问题在哪里。请帮帮忙。

1 个答案:

答案 0 :(得分:0)

是否可以显示此功能的代码$this->Cart->addProduct()

<?php
App::uses('AppModel', 'Model');
App::uses('CakeSession', 'Model/Datasource');

class Cart extends AppModel {

    public $useTable = false; 

    /*
     * add a product to cart
     */
    public function addProduct($productId) {
        $allProducts = $this->readProduct();
        if (null!=$allProducts) {
            if (array_key_exists($productId, $allProducts)) {
                $allProducts[$productId]++;
            } else {
                $allProducts[$productId] = 1;
            }
        } else {
            $allProducts[$productId] = 1;
        }

        $this->saveProduct($allProducts);
    }

    /*
     * get total count of products
     */
    public function getCount() {
        $allProducts = $this->readProduct();

        if (count($allProducts)<1) {
            return 0;
        }

        $count = 0;
        foreach ($allProducts as $product) {
            $count=$count+$product;
        }

        return $count;
    }

    /*
     * save data to session
     */
    public function saveProduct($data) {
        return CakeSession::write('cart',$data);
    }

    /*
     * read cart data from session
     */
    public function readProduct() {
        return CakeSession::read('cart');
    }

}