如何查明产品是否存在(如果存在) +数字的值
我正在寻找有关我的购物车的帮助。我使用一个简单的脚本。一切都按我想要的方式进行,但我不知道如何找出某些产品是否已经存在,如果存在,我想增加价值。脚本如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您对存储在 $_SESSION 中的购物篮的数组结构有一定的控制,我建议使用类似的东西。
此方法不需要对购物篮中的每个项目进行迭代比较来确定其实例是否已添加到购物篮中。 这将允许您轻松
添加完购物篮后,
处理购物篮。 Alex 在评论中发布的代码,问题示例而非解决方案
评论代码结束
If you've some control over the structure of the array stored in $_SESSION for your basket I'd recommend using something like this.
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
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
End of comment code