php添加购物车问题

发布于 2024-11-06 11:25:37 字数 1751 浏览 0 评论 0原文

我正在尝试创建一个 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++;
        }
    }

Im trying to create a php function that adds an item to a shopping cart. what i want it to do is check the array to see if the item is already in there, if it is increase the quantity, if not create the item in the cart.

What it is doing instead is it adding an item, it'll work the first time (if the items already there it'l just increase the quantity) but if you add another item it keeps on creating new instances of that item in the shopping cart
e.g

item 1 - quantity 4
item 2 - quantity 1
item 2 - quantity 1
item 2 - quantity 1... and so on...

below is the code i have so far?

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++;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2024-11-13 11:25:37

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

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])){
    ...
}

The problem is that you aren't searching the whole array first to see if the item is present in it. The code below should work, but I may have made a typo or something else so make sure you double check it.

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++;
        }
    }

An even better option would be to use a dictionary

ie.

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

Then you can check if the item is in the array using:

if(isset($arr[$id])){
    ...
}
ゃ懵逼小萝莉 2024-11-13 11:25:37

逻辑是有缺陷的。仅当该商品恰好是购物车中的第一个商品时,代码才会增加该商品的数量。否则,它将添加该项目。此处证明:

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 (而不是 while)循环。

The logic is flawed. The code will increment the item's quantity only if it happens to be the first item in the cart. Otherwise, it will add the item. Evidenced here:

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

Instead, you should remove the new OrderItem($id, $qty) from the loop and check the value of $added after looping through all the cart items. For this to work, you need to loop via for (instead of a while) using $count.

长安忆 2024-11-13 11:25:37

产品类扩展 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/');
}

}

class Products extends 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/');
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文