-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartAdd.php
More file actions
73 lines (53 loc) · 1.92 KB
/
cartAdd.php
File metadata and controls
73 lines (53 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
session_start();
$id = $_POST['id'];
$itemName = $_POST['itemName'];
$qty = $_POST['qty'];
$price = $_POST['price'];
$itemID = $_POST['itemID'];
if (!isset($_SESSION['cart_items'])) {
$itemArray = array();
$_SESSION['cart_items'] = $itemArray;
$_SESSION['total_price'] = 0;
}
$fid = isDuplicate($id);
if ($fid >= 0) {
//remove exsisting item form the session array
$itemArray = $_SESSION['cart_items'];
unset($itemArray[$id]); //remove index
$itemArray2 = array_values($itemArray); //reindex array
$_SESSION['cart_items'] = $itemArray2;
}
$movie = array("id" => $id, "name" => $itemName, "qty" => $qty, "price"=>$price, "itemID"=>$itemID);
$total = $_SESSION['total_price'];
$priceForMove = $qty*$price;
$_SESSION['total_price'] = $total + $priceForMove;
$itemArray = $_SESSION['cart_items']; //get available array items from the session
array_push($itemArray, $movie); //push $movie array element into $itemArray
$_SESSION['cart_items'] = $itemArray;
//this will shows how the elements store in the multidemantional array
//print_r($itemArray);
header("Location:cartExplorer.php");
//echo '<a href="cartExplorer.php">back</a>';
function isDuplicate($id) {
$fid = -1;
//is same movie selected
$itemArray = $_SESSION['cart_items'];
$itemArraylength = count($itemArray);
for ($x = 0; $x < $itemArraylength; $x++) {
if ($id == $itemArray[$x]["id"]) {
//rearrage total for substract from the total
$itemPrice = $itemArray[$x]["qty"] * $itemArray[$x]["price"];
$newTotal = $_SESSION['total_price'] - $itemPrice;
$_SESSION['total_price'] = $newTotal;
return $x;
}
}
return $fid;
}
?>