PHP购物车无法正常工作

时间:2015-12-19 15:45:59

标签: php

我的购物车功能不正常。

如果我将一个项目添加到购物车,它会相应地工作,但是当我添加第二个或多个项目时,购物车似乎很奇怪......

添加第二项时,第二项的数量从2开始。

新添加的商品的数量从购物车中存在的商品数量开始。它还会随着购物车中的商品数量而增加。

添加第三个项目后,该项目显示的次数与购物车中的项目相同...

如果项目在购物车中存在,如何更改它以添加单个项目并仅增加1?

提前致谢!

<?php
    require "core.inc.php";
    require "connect.inc.php";
?>
<html>
<head>
<meta charset="UTF-8">
<title>Winkelwagen</title>
<link rel="stylesheet" type="text/css" href="winkelwagen.css" />
<link rel="stylesheet" type="text/css" href="topAndMenuHeader.css" />
</head>

<body bgcolor="#8A8B93">
<div id="container">
<?php include "topAndMenuHeader.php"; ?>
<div id="content">
    <p class="opmaakTitel">Winkelwagen</p>
    <?php
        if ( isset($_GET['itemID']) ) {
            $itemID = $_GET['itemID'];

            $sql = "SELECT * FROM items WHERE id=$itemID";

            if ( $query = mysql_query($sql) ) {
                $numRows = mysql_num_rows($query);
                if ( $numRows == 1 ) {
                    $artID = mysql_result($query, 0, 'id');
                    $artNaam = mysql_result($query, 0, 'artNaam');
                    $artPrijs = mysql_result($query, 0, 'artPrijs');
            $artAfbeelding = mysql_result($query, 0, 'artAfbeelding');            
                    $artAantal = 1;

                    $index = -1;

                    if ( isset($_SESSION['cart']) ) {
                        unserialize(serialize($_SESSION['cart']));
                        for ( $i = 0; $i < count($_SESSION['cart']); $i++ ) 
                        {
                            if ( $_SESSION['cart'][$i]['id'] ==      
                        $_GET['itemID'] ) {
                                $index = $i;
                            } 
                            if ( $index == -1 ) {
                                array_push($_SESSION['cart'], 
            array('id'=>$artID, 'naam'=>$artNaam, 'prijs'=>$artPrijs, 
            'afbeelding'=>$artAfbeelding, 'aantal'=>$artAantal));
                            } else {
                                $_SESSION['cart'][$index]['aantal']++;
                            }
                        }
    } else {
      $_SESSION['cart']  []=array('id'=>$artID,
      'naam'=>$artNaam,'prijs'=>$artPrijs,
      'afbeelding'=>$artAfbeelding,'aantal'=>$artAantal);
                }
            }
        }
    }

    foreach ( $_SESSION['cart'] as $cart ) {
    echo '<div id="artikelSpace">';
    echo    '<div id="artikel">';
    echo        '<div id="afbeelding">';
    echo'<imgsrc="data:image/jpeg;base64,'
    .base64_encode($cart['afbeelding']).'" />';
    echo        '</div>';
    echo        '<div id="naam">';
    echo            '<p>'.$cart['naam'].'</p>';
    echo        '</div>';
    echo        '<div id="aantal">';
    echo            '<form action="winkelwagen.php" method="POST">';
    echo                'Aantal:';
    echo                '<input type="text" name="aantal" value="'
    .$cart['aantal'].'" style="width: 50px; margin: 0px 20px 0px 20px" />';
    echo                '<input type="submit" name="wijzigAantal"
    value="Wijzig" style="width: 100px;
    border-radius: 5px; font-weight: bold;" />';
    echo            '</form>';
    echo        '</div>';
    echo        '<div id="prijs">';
    echo            '<p>Prijs: €'.($cart['prijs'] * $cart['aantal']).'</p>';
    echo        '</div>';
    echo        '<div id="verwijder">';
    echo            '<ul>';
    echo                '<li><a href="#">Verwijder</a></li>';
    echo            '</ul>';
    echo        '</div>';
    echo    '</div>';
    echo '</div>';
    }
    ?>
</div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

你必须在循环的每次迭代中初始化panic: compileCallback: output parameter size is wrong 变量,如下所示:

$index

旁注:不要使用// your code if ( isset($_SESSION['cart']) ) { unserialize(serialize($_SESSION['cart'])); for ( $i = 0; $i < count($_SESSION['cart']); $i++ ) { $index = -1; // initialize $index in each iteration if ( $_SESSION['cart'][$i]['id'] == $_GET['itemID'] ) { $index = $i; } if ( $index == -1 ) { array_push($_SESSION['cart'], array('id'=>$artID, 'naam'=>$artNaam, 'prijs'=>$artPrijs, 'afbeelding'=>$artAfbeelding, 'aantal'=>$artAantal)); } else { $_SESSION['cart'][$index]['aantal']++; } } } else { $_SESSION['cart'][]=array('id'=>$artID, 'naam'=>$artNaam,'prijs'=>$artPrijs, 'afbeelding'=>$artAfbeelding,'aantal'=>$artAantal); } // your code 数据库扩展,它们在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。请改用mysql_mysqli扩展程序。这是why you shouldn't use mysql_ functions

相关问题