PHP 购物车中的多个商品

发布于 2024-09-02 23:04:53 字数 624 浏览 5 评论 0原文

我正在用 PHP 制作购物车。为了检查用户是否选择了多个产品,我将所有内容放入一个数组中($contents)。当我输出它时,我得到类似“14,14,14,11,10”的内容。我想要“3 x 14、1 x 11、1 x 10”之类的东西。最简单的方法是什么?我真的不知道该怎么做。

这是我的代码中最重要的部分。

    $_SESSION["cart"] = $cart;

    if ( $cart ) {
        $items = explode(',', $cart);
        $contents = array();
        $i = 0;
        foreach ( $items as $item ) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            $i++;
        }

        $smarty->assign("amount",$i);


        echo '<pre>';
        print_r($contents);
        echo '</pre>';

提前致谢。

I'm in the progress of making a shopping cart in PHP. To check if a user has selected multiple products, I put everything in an array ($contents). When I output it, I get something like "14,14,14,11,10". I'd like to have something like "3 x 14, 1 x 11, 1 x 10". What is the easiest way to do that? I really have no clue how to do it.

This is the most important part of my code.

    $_SESSION["cart"] = $cart;

    if ( $cart ) {
        $items = explode(',', $cart);
        $contents = array();
        $i = 0;
        foreach ( $items as $item ) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            $i++;
        }

        $smarty->assign("amount",$i);


        echo '<pre>';
        print_r($contents);
        echo '</pre>';

Thanks in advance.

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

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

发布评论

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

评论(4

╄→承喏 2024-09-09 23:04:53

为什么不构建一个更强大的购物车实施?

考虑从这样的数据结构开始:

$cart = array(
  'lines'=>array(
     array('product_id'=>14,'qty'=>2),
     array('product_id'=>25,'qty'=>1)
   )
);

或类似的。

然后,您可以创建一组对购物车结构进行操作的函数:

function addToCart($cart, $product_id, $qty){
   foreach($cart['lines'] as $line){
     if ($line['product_id'] === $product_id){
       $line['qty']  += $qty;
       return;
     }
   }
   $cart['lines'][] = array('product_id'=>product_id, 'qty'=>$qty);
   return;
}

当然,您可以(也许应该)更进一步,将此数据结构和函数组合到一组类中。购物车是开始以面向对象的方式思考的好地方。

Why not build a more robust cart implementation?

Consider starting with a data-structure like this:

$cart = array(
  'lines'=>array(
     array('product_id'=>14,'qty'=>2),
     array('product_id'=>25,'qty'=>1)
   )
);

Or similar.

Then you could create a set of functions that operate on the cart structure:

function addToCart($cart, $product_id, $qty){
   foreach($cart['lines'] as $line){
     if ($line['product_id'] === $product_id){
       $line['qty']  += $qty;
       return;
     }
   }
   $cart['lines'][] = array('product_id'=>product_id, 'qty'=>$qty);
   return;
}

Of course, you could (and perhaps should) go further and combine this data structure and functions into a set of classes. Shopping carts are a great place to start thining in an object-oriented way.

淡淡的优雅 2024-09-09 23:04:53

内置的 array_count_values 函数可能可以完成这项工作。

例如:

<?php
$items = array(14,14,14,11,10);
var_dump(array_count_values($items));
?>

输出:

array(3) {
  [14]=>
  int(3)
  [11]=>
  int(1)
  [10]=>
  int(1)
}

The built-in array_count_values function might does the job.

E.g:

<?php
$items = array(14,14,14,11,10);
var_dump(array_count_values($items));
?>

Outputs:

array(3) {
  [14]=>
  int(3)
  [11]=>
  int(1)
  [10]=>
  int(1)
}
逆蝶 2024-09-09 23:04:53

使用多维数组以更稳健的结构存储数据将使您受益匪浅。

例如:

$_SESSION['cart'] = array(
  'lines'=>array(
     array('product_id'=>14,'quantity'=>2, 'item_name'=>'Denim Jeans'),
     ...
   )
);

然后要将新商品添加到购物车,您可以简单地执行以下操作:

$_SESSION['cart'][] = array('product_id'=45,'quantity'=>1, 'item_name'=>'Jumper');

You would benefit from using a multi dimensional array to store your data in a more robust structure.

For example:

$_SESSION['cart'] = array(
  'lines'=>array(
     array('product_id'=>14,'quantity'=>2, 'item_name'=>'Denim Jeans'),
     ...
   )
);

Then to add new items to the cart you can simply do this:

$_SESSION['cart'][] = array('product_id'=45,'quantity'=>1, 'item_name'=>'Jumper');
听闻余生 2024-09-09 23:04:53

当您让用户添加项目时,您需要将其添加到数组中的正确位置。如果数组中已经存在产品 id,则需要更新它。还要始终小心尝试输入零或负数的用户!

When you let a user add an item you need to add it in the right position in the array. If the product id already exist in the array, you need to update it. Also always be careful of users trying to enter zero or minus numbers!

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