如何在php中创建在线购物车 [英] How to create an online shopping cart in php

查看:53
本文介绍了如何在php中创建在线购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个购物车,它有四个按钮继续购物、清除购物车、更新购物车和下订单我遇到了会话问题,例如当我按下清除购物车按钮时,它会显示

 未定义的索引购物车

但是购物车来自会话,当我按下清除购物车按钮时

它取消购物车的会话

那我哪里做错了?有人可以帮忙吗?这是我的代码

0){remove_product($_REQUEST['pid']);}else if($_REQUEST['command']=='clear'){未设置($_SESSION['购物车']);}else if($_REQUEST['command']=='update'){$max=count($_SESSION['cart']);for($i=0;$i<$max;$i++){$pid=$_SESSION['cart'][$i]['productid'];$q=intval($_REQUEST['product'.$pid]);if($q>0 && $q<=999){$_SESSION['cart'][$i]['qty']=$q;}别的{$msg='有些产品没有更新!,数量必须是1到999之间的数字';}}}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>购物车</title><脚本语言="javascript">函数德尔(pid){if(confirm('你真的要删除这个项目吗')){document.form1.pid.value=pid;document.form1.command.value='delete';document.form1.submit();}}函数 clear_cart(){if(confirm('这将清空你的购物车,继续吗?')){document.form1.command.value='clear';document.form1.submit();}}函数 update_cart(){document.form1.command.value='更新';document.form1.submit();}<身体><form name="form1" method="post"><input type="hidden" name="pid"/><input type="hidden" name="command"/><div style="margin:0px auto; width:600px;"><div style="padding-bottom:10px"><h1 align="center">您的购物车</h1><input type="button" value="继续购物" onclick="window.location='products.php'"/>

<div style="color:#F00"><?php echo'' ;?></div><table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%"><?phpif(is_array($_SESSION['cart'])){echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>系列</td><td>名称</td><td>价格</td><;td>数量</td><td>数量</td><td>选项</td></tr>';$max=count($_SESSION['cart']);for($i=0;$i<$max;$i++){$pid=$_SESSION['cart'][$i]['productid'];$q=$_SESSION['cart'][$i]['qty'];$pname=get_product_name($pid);if($q==0) 继续;?><tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>;<td>$ <?php echo get_price($pid)?></td><td><input type="text" name="product<?php echo $pid?>"value="<?php echo $q?>"maxlength="3" size="2"/></td><td>$ <?php echo get_price($pid)*$q?></td><td><a href="javascript:del(<?php echo $pid?>)">删除</a></td></tr><?php}?><tr><td><b>订单总数:$<?php echo get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="更新购物车" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='billing.php'"></td></tr><?php}别的{echo "<tr bgColor='#FFFFFF'><td>您的购物车中没有商品!</td>";}?>

</表单></html>

解决方案

如果您还没有开始填充您的购物车,那么该元素将是未定义的.在处理数组元素时,最好添加额外的测试 array_key_exists().但是,在您的情况下,问题更多地在于您通常处理数组的方式.

PHP 非常宽容,因此您必须在编码方式上强制执行自己的纪律.一个值得使用的非常好的实践是在开始使用变量之前显式初始化它们.

//靠近代码顶部的某处$_SESSION['cart'] = array_key_exists('cart', $_SESSION)?$_SESSION['cart']:array();

并且不要在清除测试中取消设置......只需将其设置回一个空数组.

else if($_REQUEST['command']=='clear'){$_SESSION['cart'] = array();}

I am creating a shopping cart which has four buttons continue shopping, clear cart, update cart and place order I am facing a problem with the sessions like when I press the clear cart button it shows the

 undefined index cart 

but the cart comes from the session and when i press the clear cart button

it unsets the session of cart 

so where i am doing it wrong ? can anyone help ? here is my code

<?php
    include("includes/db.php");
    include("includes/functions.php");

    if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
        remove_product($_REQUEST['pid']);
    }
    else if($_REQUEST['command']=='clear'){
        unset($_SESSION['cart']);
    }
    else if($_REQUEST['command']=='update'){
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=999){
                $_SESSION['cart'][$i]['qty']=$q;

            }
            else{
                $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
            }
        }
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }


</script>
</head>

<body>
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
    <div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
        <h1 align="center">Your Shopping Cart</h1>
    <input type="button" value="Continue Shopping" onclick="window.location='products.php'" />
    </div>
        <div style="color:#F00"><?php echo'' ;?></div>
        <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
        <?php
            if(is_array($_SESSION['cart'])){
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
                $max=count($_SESSION['cart']);
                for($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname=get_product_name($pid);
                    if($q==0) continue;
            ?>
                    <tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>
                    <td>$ <?php echo get_price($pid)?></td>
                    <td><input type="text" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /></td>                    
                    <td>$ <?php echo get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?php echo $pid?>)">Remove</a></td></tr>
            <?php                   
                }
            ?>
                <tr><td><b>Order Total: $<?php echo get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='billing.php'"></td></tr>
            <?php
            }
            else{
                echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
            }
        ?>
        </table>
    </div>
</form>
</body>
</html>

解决方案

If you haven't already started populating your cart then the element will be undefined. When dealing with array elements it's always better to add the additional test array_key_exists(). However in your case the problem is more about the way you handle your array generally.

PHP is quite forgiving, so you have to enforce your own disciplines on the way you code. One piece of very good practice which is worth getting used is to explicitly initialise your variables before you start using them.

// somewhere near the top of your code
$_SESSION['cart'] = array_key_exists('cart', $_SESSION)?$_SESSION['cart']:array();

and don't unset it in the clear test ... just set it back to an empty array.

else if($_REQUEST['command']=='clear'){
    $_SESSION['cart'] = array();
}

这篇关于如何在php中创建在线购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆