【问题标题】:Php add to shopping cart problemphp添加到购物车问题
【发布时间】:2011-08-21 19:42:05
【问题描述】:

我正在尝试创建一个将商品添加到购物车的 php 函数。我想要它做的是检查数组以查看该项目是否已经在那里,如果它增加数量,如果不是在购物车中创建该项目。

它所做的是添加一个项目,它会在第一次工作(如果项目已经存在,它只会增加数量)但是如果你添加另一个项目,它会继续创建该项目的新实例在购物车中 例如

项目 1 - 数量 4 项目 2 - 数量 1 项目 2 - 数量 1 项目 2 - 数量 1...等等...

下面是我目前的代码?

function add_item ($id, $qty)
    {
        $count=$this->countItems;
        echo  "uytfdgghjkl;kj<br>";
        $added = false;
        if($count>0)
        {
            $i=0;
            while($added == false)
            {
                echo "fghjkl<br>";
                $tid = $this->items[$i]->getId();
                echo "new ID: ".$tid."<br>";
                echo "old ID: ".$id."<br>";
                echo $i;
                if($tid == $id)
                {
                    $amount = $this->items[$i]->getQty();
                    $this->items[$i]->setQty($amount+1);
                    $added = true;
                    //$i++;
                    //break;
                }
                if($added == true)
                {
                    break;
                }
                else //if($added == false)
                {
                    $this->items[$this->countItems] = new OrderItem($id, $qty);
                    //$this->total = $total+ ($qty *$price);
                    $this->countItems++;
                    $added = true;
                    //break;
                }
                //else break;
                $i++;
            }

        }
        else
        {
            $this->items[$this->countItems] = new OrderItem($id, $qty);
            //$this->total = $total+ ($qty *$price);
            $this->countItems++;
        }
    }

【问题讨论】:

  • 这段代码有很多错误。但是对于初学者来说,您永远不必检查重复项 - 如果使用 SKU 作为索引键不是不言而喻的,那么您需要花费更多时间来学习和实施算法。

标签: php oop shopping-cart


【解决方案1】:

类 Products 扩展 CI_Controller{

function  __construct(){
    parent::__construct();

    // Load cart library
    $this->load->library('cart');

    // Load product model
    $this->load->model('product');
}

function index(){
    $data = array();

    // Fetch products from the database
    $data['products'] = $this->product->getRows();

    // Load the product list view
    $this->load->view('products/index', $data);
}

function addToCart($proID){

    // Fetch specific product by ID
    $product = $this->product->getRows($proID);

    // Add product to the cart
    $data = array(
        'id'    => $product['id'],
        'qty'    => 1,
        'price'    => $product['price'],
        'name'    => $product['name'],
        'image' => $product['image']
    );
    $this->cart->insert($data);

    // Redirect to the cart page
    redirect('cart/');
}

}

【讨论】:

    【解决方案2】:

    逻辑有问题。只有当它恰好是购物车中的第一个项目时,代码才会增加项目的数量。否则,它将添加该项目。证明在这里:

    if($added == true) // if this cart item's quantity was incremented
    {
        break;
    }
    else // add item
    {
        $this->items[$this->countItems] = new OrderItem($id, $qty);
        // etc...
    }
    

    相反,您应该从循环中删除new OrderItem($id, $qty),并在遍历所有购物车项目后检查$added 的值。为此,您需要使用$count 循环通过for(而不是一段时间)。

    【讨论】:

      【解决方案3】:

      问题是您没有首先搜索整个数组以查看该项目是否存在于其中。下面的代码应该可以工作,但我可能打错字或其他问题,所以请务必仔细检查。

      function add_item ($id, $qty)
          {
              $count=$this->countItems;
              echo  "uytfdgghjkl;kj<br>";
              $added = false;
              if($count>0)
              {
                  for($i=0; $i < $count; $i++)
                  {
                      echo "fghjkl<br>";
                      $tid = $this->items[$i]->getId();
                      echo "new ID: ".$tid."<br>";
                      echo "old ID: ".$id."<br>";
                      echo $i;
                      if($tid == $id)
                      {
                          $amount = $this->items[$i]->getQty();
                          $this->items[$i]->setQty($amount+1);
                          $added = true;
                          break;
                      }
                  }
      
              }
              if(!$added)
              {
                  $this->items[$this->countItems] = new OrderItem($id, $qty);
                  //$this->total = $total+ ($qty *$price);
                  $this->countItems++;
              }
          }
      

      更好的选择是使用字典

      即。

      $arr = array();
      $arr['item_id'] = new OrderItem(...);
      

      然后您可以使用以下方法检查该项目是否在数组中:

      if(isset($arr[$id])){
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        相关资源
        最近更新 更多