2016-08-18 70 views
0

我希望在數據不同但具有相同標識符的情況下始終開啓複製會話(ID)。¿如何複製會話?

產品數據庫的結構:

id product price 
1  shirt 30 
2  pants 34 

產品的顏色的數據庫的結構:

id product_id color 
1  1   blue 
2  1  yellow 
3  2   White 
4  2   black 

的數據庫的結構尺寸的產品

id product_id size 
1  1   XS 
2  1   S 
3  2   M 
4  2   XL 

數據(colorsize)通過方法POST

$_SESSION['colors'][$itemId]=$_POST['colors']; 
$_SESSION['size'][$itemId]=$_POST['size']; 

和產品的id通過方法GET發送。

$itemId = isset($_GET['itemId']) ? $_GET['itemId'] : ""; 

我需要複製產品的會話以選擇不同的顏色或大小。

----------------------------------- 
Product  Quantity  Price 
----------------------------------- 
Shirt blue S  1   30 
Shirt blue XS  1   30 
Shirt yellow S 1   30 

如果我們用相同的值(藍色襯衫S)添加相同的產品,僅更新量。

----------------------------------- 
Product  Quantity  Price 
----------------------------------- 
Shirt blue S  2   60 

不成就獲得了產品的複製,更新選擇的相同值 。

代碼updatecart.php

<?php 
session_start(); 

$itemId = isset($_GET['itemId']) ? $_GET['itemId'] : ""; 

//SESSION colors 
$_SESSION['colors'][$itemId]=$_POST['colors']; 
//SESSION size 
$_SESSION['size'][$itemId]=$_POST['size']; 

if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['qtyupdate'])) { 
    for ($i = 0; $i < count($_POST['qtyupdate']); $i++) { 
     $key = $_POST['arr_key_' . $i]; 
     $_SESSION['qty'][$key] = $_POST['qtyupdate'][$i]; 
    } 

} else { 
    $qty = isset($_POST['qty']) ? $_POST['qty'] : 1; 
    if (!isset($_SESSION['cart'])) { 
     $_SESSION['cart'] = array(); 
     $_SESSION['qty'][] = array(); 
    } 
    if (in_array($itemId, $_SESSION['cart'])) { 
     $key = array_search($itemId, $_SESSION['cart']); 
     $_SESSION['qty'][$key] = $_SESSION['qty'][$key] + $qty; 
    } else { 
     array_push($_SESSION['cart'], $itemId); 
     $key = array_search($itemId, $_SESSION['cart']); 
     $_SESSION['qty'][$key] = $qty; 
    } 
} 
header('location:cart.php'); 
?> 

代碼cart.php

<?php 
session_start(); 
$itemCount = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0; 
if (isset($_SESSION['qty'])){ 
    $meQty = 0; 
    foreach ($_SESSION['qty'] as $meItem){ 
     $meQty = $meQty + $meItem; 
    } 
}else{ 
    $meQty = 0; 
} 

if (isset($_SESSION['cart']) and $itemCount > 0){ 
    $itemIds = ""; 
    foreach ($_SESSION['cart'] as $itemId){ 
     $itemIds = $itemIds . $itemId . ","; 
    } 
    $inputItems = rtrim($itemIds, ","); 
    $meSql = "SELECT * FROM products WHERE id in ({$inputItems})"; 
    $meQuery = mysqli_query($kcon, $meSql); 
    $meCount = mysqli_num_rows($meQuery); 
}else{ 
    $meCount = 0; 
} 

if ($meCount == 0){ 
    echo "<div>No items in the basket</div>"; 
}else{ 
?> 

    <form action="updatecart.php" method="post" name="fromupdate">    
    <table class="table"> 
    <tr> 
    <td>Product</td> 
    <td>Price</td> 
    <td>Quantity</td> 
    </tr> 
    <?php 
    $total_price = 0; 
    $num = 0; 
    while ($meResult = mysqli_fetch_assoc($meQuery)){ 
     $key = array_search($meResult['id'], $_SESSION['cart']); 
     $total_price = $total_price + ($meResult['price'] * $_SESSION['qty'][$key]); 
     $intemId=$meResult['id']; 
     $color = $_SESSION['colors'][$intemId]; 
     $size = $_SESSION['size'][$intemId]; 
    } 
    ?> 
    <tr> 
    <td class="cart-image"> 
    <h3><?php echo $meResult['product']; ?> <?php echo $color; ?> <?php echo $size?></h3> 
    </td> 
    <td class="cart-price"><?php echo $meResult['price']; ?></td> 
    <?php 
    $num++; 
    } 
    ?>          
    </tr> 
    </table> 

請幫助我,一個產品的會話複製到選擇一種顏色或大小不同。

謝謝。

回答

0

你應該使用像這樣的多維數組它不會覆蓋你的值。

$ _SESSION ['colors'] [] [$ itemId] = $ _ POST ['colors'];

$ _SESSION ['size'] [] [$ itemId] = $ _ POST ['size'];

+0

感謝您的建議,併爲複製產品的會話選擇顏色或大小不同??? –

0

我認爲你的情況使用數據庫而不是會話。 創建一個表並插入用戶的不同選擇並處理該表以供計算。會話的大量使用將是頭痛的問題。

+0

我知道會話的大量使用會令人頭疼。但不建立一個表。至少在會話中創建一個小例子? –