如何查明产品是否存在(如果存在) +数字的值

发布于 2024-12-29 14:21:43 字数 1038 浏览 0 评论 0原文

我正在寻找有关我的购物车的帮助。我使用一个简单的脚本。一切都按我想要的方式进行,但我不知道如何找出某些产品是否已经存在,如果存在,我想增加价值。脚本如下:

for ($x=0;$x<count( $_SESSION["sess_name"]);$x++) {
    $x_id= $_SESSION["sess_id"];
    $x_name_prd= $_SESSION["sess_name_prd"];
    $x_number_prd= $_SESSION["sess_number_prd"];

   //if there is a product that is already in the cart then go to function check_me(); to do something to make the sess_number_prd +1 everytimes people click on the button.
    if ( $x_name_prd[$x]==$name_prd){
        check_me($x_name_prd[$x]);
    }
}

我已经尝试过这种方法,但不起作用

function check_me ($name_prd)  {
    for ($i=0;$i<count($_SESSION['sess_name']);$i++) {
        if (!in_array($_SESSION['sess_id'][$i],$)) {
            $temp_id[]=$_SESSION['sess_id'][$i];
            $temp_name[]=$_SESSION['sess_name_prd'][$i];

            $temp_num[]=+1;
        }
    }
    $_SESSION['sess_id']=$temp_id;
    $_SESSION['number_prd']=+1;
    $_SESSION['sess_name']=$temp_name;
} 

有人可以帮助我吗?

I´m looking for some help about my shopping cart. I use a simple script. Everything works as I want, but I don´t know how to find out if some products already exist, if they do I want to increment the value. The script is below:

for ($x=0;$x<count( $_SESSION["sess_name"]);$x++) {
    $x_id= $_SESSION["sess_id"];
    $x_name_prd= $_SESSION["sess_name_prd"];
    $x_number_prd= $_SESSION["sess_number_prd"];

   //if there is a product that is already in the cart then go to function check_me(); to do something to make the sess_number_prd +1 everytimes people click on the button.
    if ( $x_name_prd[$x]==$name_prd){
        check_me($x_name_prd[$x]);
    }
}

I have tried this method, but does not work

function check_me ($name_prd)  {
    for ($i=0;$i<count($_SESSION['sess_name']);$i++) {
        if (!in_array($_SESSION['sess_id'][$i],$)) {
            $temp_id[]=$_SESSION['sess_id'][$i];
            $temp_name[]=$_SESSION['sess_name_prd'][$i];

            $temp_num[]=+1;
        }
    }
    $_SESSION['sess_id']=$temp_id;
    $_SESSION['number_prd']=+1;
    $_SESSION['sess_name']=$temp_name;
} 

Can someone help me please..

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

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

发布评论

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

评论(1

分開簡單 2025-01-05 14:21:43

如果您对存储在 $_SESSION 中的购物篮的数组结构有一定的控制,我建议使用类似的东西。

/*Entry point
  It is assumed at this point you have the value product id ($pid) and initiated 
  your $_SEESION[<basket array index>] set - "basket" in this example
*/
(isset($_SESSION['basket'][$pid]))? $_SESSSION['basket'][$pid]++ : $_SESSION['basket'][$pid] = 1;

此方法不需要对购物篮中的每个项目进行迭代比较来确定其实例是否已添加到购物篮中。 这将允许您轻松

foreach($_SESSION['basket'] as $product_id => $noOfProduct){
     /*Do your checkout thing here*/
}

添加完购物篮后,

处理购物篮。 Alex 在评论中发布的代码,问题示例而非解决方案

for ($x=0;$x<count( $_SESSION["sess_id"]);$x++){
    $x_id= $_SESSION["sess_id"][$x];    //[$x] appended, presumed missing
    $x_id_prd= $_SESSION["sess_id_prd"];
    $x_name_prd= $_SESSION["sess_name"]; 
    $x_size_prd= $_SESSION["sess_size"]; 
    $x_color_prd= $_SESSION["sess_color"];
    $x_clr_code_prd= $_SESSION["sess_clr_code"]; 
    $x_num_prd= $_SESSION["sess_num"]; 
    if($x_id_prd[$x]==$id_prd 
      && $x_size_prd[$x]==$size_prd 
      && $x_color_prd[$x]==$color_prd 
      && $x_clr_code_prd[$x]==$clr_code ){ 
        //do something to get $_SESSION["sess_num"]+1;
    } 
}

评论代码结束

If you've some control over the structure of the array stored in $_SESSION for your basket I'd recommend using something like this.

/*Entry point
  It is assumed at this point you have the value product id ($pid) and initiated 
  your $_SEESION[<basket array index>] set - "basket" in this example
*/
(isset($_SESSION['basket'][$pid]))? $_SESSSION['basket'][$pid]++ : $_SESSION['basket'][$pid] = 1;

This approach doesn't require the iterative comparison of each item in the basket to determine if an instance of it has already been added to the basket. This will allow for a simple

foreach($_SESSION['basket'] as $product_id => $noOfProduct){
     /*Do your checkout thing here*/
}

to process the basket once you're finished adding to it.

Code posted in the comments by Alex, example of the problem not a solution

for ($x=0;$x<count( $_SESSION["sess_id"]);$x++){
    $x_id= $_SESSION["sess_id"][$x];    //[$x] appended, presumed missing
    $x_id_prd= $_SESSION["sess_id_prd"];
    $x_name_prd= $_SESSION["sess_name"]; 
    $x_size_prd= $_SESSION["sess_size"]; 
    $x_color_prd= $_SESSION["sess_color"];
    $x_clr_code_prd= $_SESSION["sess_clr_code"]; 
    $x_num_prd= $_SESSION["sess_num"]; 
    if($x_id_prd[$x]==$id_prd 
      && $x_size_prd[$x]==$size_prd 
      && $x_color_prd[$x]==$color_prd 
      && $x_clr_code_prd[$x]==$clr_code ){ 
        //do something to get $_SESSION["sess_num"]+1;
    } 
}

End of comment code

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