购物车数据未在html表中显示

时间:2015-12-06 12:06:15

标签: php html mysql

我制作了一个简单的购物车,我想在购物车中显示所选商品。当我print_r我的数组显示数据时,我可以成功选择项目。即使session['cart']的数量也增加了。但不幸的是,它并没有在我的侧边栏表格中显示数据。这是我的代码。请给我任何建议。我坚持这个。 JSFiddle. First entry update quantity

代码

     <?php

        if(isset($_GET['action']) && $_GET['action']=="add"){
           $id=intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
    //$index=$id.$color.$size;
$sql_p="SELECT * FROM products WHERE productid={$id}";

$query_p=mysqli_query($con, $sql_p);
if(mysqli_num_rows($query_p)!=0){
    $row_p=mysqli_fetch_array($query_p);
if(isset($_SESSION['cart'][$id])  && isset($_SESSION['cart'][$id]['color']) && $_SESSION['cart'][$id]['color'] == $color && isset($_SESSION['cart'][$id]['size']) && $_SESSION['cart'][$id]['size'] == $size)
        {
            $_SESSION['cart'][$id]['quantity']++;
           // $_SESSION['cart'][$index] = array('quantity' => $_SESSION['cart'][$id]['quantity'], 'price' => $row_p['product_price'], 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row_p['product_name']);

            print_r($_SESSION['cart'][$id]);
            echo "\n";
            //print_r($_SESSION['cart'][$index]);

            echo "<script>alert('".$row_p['product_name']." has been added to your cart.');</script>";
}
else{

   // $_SESSION['cart'][$index]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=> $color,"size"=> $size,"name"=>$row_p['product_name']);
     $_SESSION['cart'][$row_p['productid']]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=> $color,"size"=> $size,"name"=>$row_p['product_name']);
       print_r($_SESSION['cart'][$row_p['productid']]);
    }
}
}
?>

购物车表

     <div id="sidebar">
            <h1>Cart:   <?php echo  count($_SESSION['cart']); ?></h1>
            <table border="1">
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Quantity</th>
                    <th>color</th>
                    <th>size</th>
                    <th></th>
                </tr>
                <?php
                if(isset($_SESSION['cart'])){

                        $sql="Select * from products where productid IN (";
                        foreach($_SESSION['cart'] as $id => $value){
                            $sql .=$id. ",";
                        }

                        $sql=substr($sql,0,-1) . ") ORDER BY productid ASC";
                        $query = mysqli_query($con, $sql);
                        if(!empty($query)){
                            while($row = mysqli_fetch_array($query)){

                                ?>




                        <tr>
                            <td><?php echo $row['productid'] ?></td>
                            <td><?php echo $row['product_name']?></td>
                       <td><?php echo $_SESSION['cart'][$row['productid']]['quantity']; ?></td>
                            <td><?php echo  $_SESSION['cart'][$row['productid']]['color']?></td>
                            <td><?php echo $_SESSION['cart'][$row['productid']]['size'] ?></td>
                            <td><a href="index.php?page=remove_from_cart&action=remove&id=<?php echo $row['productid']; ?>">x</a> </td>
                        </tr>

                    <?php

                    }
                    }
                    ?>

          <tr><td colspan="3"><a href="index.php?page=cart">Go To Cart</a></td></tr> <tr><td><?php echo "Your Cart is Empty"; ?></td></tr>

        </table>
      </div>

0 个答案:

没有答案