繁体   English   中英

购物车中的会话

[英]Sessions in shopping cart

我试图在表中显示会话中存储的值,问题是:如何显示所有信息,直到现在该应用程序显示了我的三个会话之一,其余的呢? 任何想法?

<?php
    $_SESSION['id'][] = $_GET['id'];
    $_SESSION['name'][] = $_GET['name'];
    $_SESSION['price'][] = $_GET['price'];
?>

<h1>Shopping Cart</h1><br>
<table border=1>
    <th>ID</th>
    <th>Name</th>
    <th>Price</th>
    <tbody id="tb">
    <?php foreach($_SESSION['name'] as $key=> $n){ ?>
    <tr>
        <td><?php ?></td>   
        <td><?php echo $n; ?></td>  
        <td><?php ?></td>               
    </tr>
   <?php } ?>
</tbody>
</table>

当用户单击注销按钮时,您将释放SESSION值并将警报弹出给用户以清除购物车项目

 session_destroy();
 OR
 // remove all session variables
session_unset(); 

也许您需要更改为:

<?php
     $product = array(
         'id' => $_GET['id'],
         'name' => $_GET['name'],
         'price' =>  $_GET['price'],
     );
     $_SESSION['product'] = $product;
?>

<h1>Shopping Cart</h1><br>
<table border=1>
    <thead>
         <tr>
             <th>ID</th>
             <th>Name</th>
             <th>Price</th>
         </tr>
    </thead>
    <tbody id="tb">
        <?php if isset($_SESSION['product']): ?>
        <tr>
             <td><?php echo $_SESSION['product']['id']; ?></td>   
             <td><?php echo $_SESSION['product']['name']; ?></td>   
             <td><?php echo $_SESSION['product']['price']; ?></td>    
        </tr>
        <?php endif; ?>
    </tbody>
</table>

如果您需要在您的应用中支持多种产品:

<?php
    // you can check that the cart exists, if not, create it.
     if (!isset($_SESSION['cart']){
        $_SESSION['cart'] = array(
            'products' => array(),
        );
     }
     $product = array(
         'id' => $_GET['id'],
         'name' => $_GET['name'],
         'price' =>  $_GET['price'],
     );
    //add 1 product to your cart
     $_SESSION['cart']['products'][] = $product;
?>

<h1>Shopping Cart</h1><br>
<table border=1>
    <thead>
         <tr>
             <th>ID</th>
             <th>Name</th>
             <th>Price</th>
         </tr>
    </thead>
    <tbody id="tb">
        <?php if isset($_SESSION['cart']): ?>
          //$product is only 1 product in the cart
            <?php foreach ($_SESSION['cart']['products'] as $product): ?>
            <tr>
                <td><?php echo $product['id']; ?></td>   
                <td><?php echo $product['name']; ?></td>   
                <td><?php echo $product['price']; ?></td>    
            </tr>
        <?php else: ?>
            <tr>
                <td>No products</td>    
            </tr>
        <?php endif; ?>
        </tr>
    </tbody>
</table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM