[英]unset array value and key in session
我幾乎完成了購物車的制作,但是我不確定如何從購物車中刪除物品。 我正在使用存儲在會話中的數組。 所以我想取消設置數組中的所選項目(使用我添加的復選框)。
products.php
cat<a href ="Adding-to-cart.php?id=2222"> add to cart<a/><br>
dog<a href ="Adding-to-cart?id=1111"> add to cart<a/><br>
bird<a href ="Adding-to-cart?id=5555"> add to cart<a/><br>
加入購物車
code
<?php
session_start();
if(empty($_SESSION['animals']))
{
$_SESSION['animals'] = array();
}
$_SESSION['animals']["".$_GET['id'].""] = 1;
?>
cart.php
<?php
//////////////////////////////////////////////
$con = mysql_connect("localhost","root","");
mysql_select_db("items",$con);
//////////////////////////////////////////////
session_start();
// create an array
$my_array=array();
// put the array in a session variable
if(!isset($_SESSION['animals']))
$_SESSION['animals']=$my_array;
// move submit code outside of foreach loop
if (isset($_POST["submit"]))
{
for ($i = 0; $i < count($_POST['aaa']); $i++) {
$aaa = $_POST['aaa'][$i];
$key_var = $_POST['ke'][$i];
// setting the session spesific session array value different for each key
$_SESSION['animals'][$key_var] = $aaa;
}
////removing checked item from shopping cart
if (isset($_POST['brosrs'])) {
/////////////Things that I have tried/////////////
////unset($_SESSION['animals'][$key_var]);///////
//unset($_SESSION['animals'][$to_unset]);
}
////removing checked item from shopping cart/////
////////////////////////////////////////////////
}
?>
<table >
<tr >
<td ><b>ID</b></td>
<td ><b>Name</b></td>
<td ><b>Price</b></td>
<td ><b>Subtotal</b></td>
<td ><b>Product ID is</b></td>
<td ><b>Quantity</b></td>
</tr>
<form method="post" action="">
<?php
//// declate the total price and start it as 0 coz its before the actual items added to cart
$total_price=0;
$subtotal = 0;
// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
{
////////////////////////////DUMPING EVERYTHING FROM DATABASE////////////////////////////////////////////////
$key_array = array_keys($_SESSION['animals']);
// Get record where $key exists.
$sql = "SELECT id, name, price FROM products WHERE id IN ({$key}) ORDER BY name";
$myData = mysql_query($sql,$con);
// Loop through each record and see if $key_array is present in $row['id'] db
while($row = mysql_fetch_array($myData)){
if(in_array($row['id'], $key_array)){
// display records
echo "<tr>";
echo "<td >". $row['id']. "</td>";
echo "<td class='name'>".$row['name']. "</td>";
echo "<td class='qtyid'>". $row['price']. "</td>";
echo "<br/>";
$quantity_calc = $value;
$subtotal = $value * $row['price'];
$price = $row['price'];
echo "<td>" . $subtotal . "</td>";
}
$total_price += $subtotal;
}
////////////////////////////DUMPING EVERYTHING FROM DATABASE////////////////////////////////////////////////
// and print out the values quantity
echo " <td> Product ID is " .$key. " Quantity is </td>";
// getting the updated value from input box
?>
<!-- <td ><input type="text" name="aaa[]" value="<?php echo $value ; ?>" size="2" /></td> -->
<td> <input id="numberinputsize" type="number" size="1" name="aaa[]" min="1" max="10" value="<?php echo $value ; ?>" > </td>
<!-- take a hidden input with value of key -->
<td ><input type="hidden" name="ke[]" value="<?php echo $key; ?>"><br></td>
<!------------------------adding a remove item checkbox ------------------------------>
<!------------------------adding a remove item checkbox ------------------------------>
<td> <input type ="checkbox" name="brosrs" value = "<?php echo $key; ?>"><?php echo $key; ?></input> </td>
<!------------------------adding a remove item checkbox ------------------------------>
<!------------------------adding a remove item checkbox ------------------------------>
<?php
echo "</tr>";
}
?>
<tr>
<td>Total amount $<?php Echo $total_price; ?></td>
<td><a href="products-legit.php">back to shopping</a></td>
<td> <input class="inputbox" type="submit" value="Update value of key" name="submit"/></td>
</tr>
</table>
</form>
<style>
table {
width:1000px;
}
</style>
謝謝!
更新! 抱歉,但是為了讓大家說清楚,我有一個產品頁面,我在其中添加id到數組中作為存儲在會話中的鍵。 單擊添加到購物車后,用戶將被重定向到Adding-to-cart.php頁面,在該頁面中將數組插入到會話中。 然后,當您上購物車時,您可以看到這些物品。 問題是>>>當我的購物車中有2件或更多商品時,移除第一件商品,而購物車中的最后一件商品則被移除。
這是演示如何擁有一個想法,您需要以自己的方式更改實現
cart.php
<?php
ob_start();
session_start();
$_SESSION['data']=array('Cat','dog','bird');
//echo "<pre>"; print_r($_SESSION['data']);exit;
?>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("#remove").on('click',function(){
var toRemove=$('#toRemove').val();
$.ajax('goToRemove.php',{
data:{"toRemove":toRemove}
});
});
});
</script>
</head>
<body>
<form name="add" id="frmAdd" method="post" action="add.php?action=add" enctype="multipart/form-data">
<input type="text" name="toRemove" value="" id="toRemove"/>
<input type="button" name="add" id="remove" value="ADD">
</form>
</body>
</html>
goToRemove.php
<?php
ob_start();
session_start();
echo "<pre>"; print_r($_SESSION['data']);
$to_delete=array_search($_REQUEST['toRemove'], $_SESSION['data']);
unset($_SESSION['data'][$to_delete]);
echo "<pre>";print_r($_SESSION);
?>
我假設您的購物車是一個多維數組,因為您沒有給出數據示例。 一種從購物車中查找和刪除商品的簡便方法是將array_search()與array_column()結合使用;
$cart = array( array('id'=>1,'name'=>'Awesome sauce'), array('id'=>2,'name'=>'Not so awesome sauce'), array('id'=>3,'name'=>'Really awesome sauce'), ); //search $cart array for the value of 2 in the id key of the array and unset it. unset($cart[array_search(2, array_column($cart, 'id'))]); print_r($cart); /* OUTPUT: Array ( [0] => Array ( [id] => 1 [name] => Awesome sauce ) [2] => Array ( [id] => 3 [name] => Really awesome sauce ) ) */ /*******Indexed array****************/ $cart = array( '2222' => 1 ,'1111' => 1 ,'6666' => 1 ,'5555' => 1 ); unset($cart['2222']); print_r($cart); /* OUTPUT: Array ( [1111] => 1 [6666] => 1 [5555] => 1 ) */
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.