簡體   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