-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
99 lines (90 loc) · 2.88 KB
/
index.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
require_once('config.php');
//session
session_start();
//create empty array for cart
if(!isset($_SESSION['tedi'])){
$_SESSION['tedi'] = array();
}
//get current exchange rate
if(!isset($_SESSION['exr'])){
$url = "https://www.bitstamp.net/api/v2/ticker/BTCUSD";
$json = json_decode(file_get_contents($url), true);
if(array_key_exists("last",$json)){
$price = $json["last"];
$_SESSION['exr'] = $price;
}
else
{
//likely an error try another source
$url = "https://blockchain.info/stats?format=json";
$json = json_decode(file_get_contents($url), true);
if(array_key_exists("market_price_usd",$json)){
$price = $json["market_price_usd"];
$_SESSION['exr'] = $price;
} else {
//both sources unsuccesful
die("Oops please try refreshing or try again later");
}
}
}
//count items in array
$cartItems = count($_SESSION['tedi']);
$cart = $_SESSION['tedi'];
$queryProducts2 ="SELECT * FROM products WHERE in_stock > 0 ORDER BY id ASC";
$resultH2=mysqli_query($conn, $queryProducts2) or die (mysqli_error($conn));
while($outputsH2=mysqli_fetch_assoc($resultH2)){
if(isset($_POST[$outputsH2['id']])){
array_push($_SESSION['tedi'], $outputsH2['id']);
$cartItems = count($_SESSION['tedi']);
$cart = $_SESSION['tedi'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Shop</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1><?php echo $storeName; ?></h1>
<div id="cartCont">
<div id="cartHeader">Your Cart</div>
<div id="cartSpace">
<?php
$usdOwed = 0;
for($i=0; $i<$cartItems; $i++)
{
$queryLoopCart = "SELECT * FROM products WHERE id = '$cart[$i]'";
$doLoopCart = mysqli_query($conn, $queryLoopCart);
$rowLoopCart = mysqli_fetch_assoc($doLoopCart);
$loopName = $rowLoopCart['name'];
$loopPrice = $rowLoopCart['price'];
$usdOwed += $loopPrice;
echo $loopName."<span class='cartPrice'>$".$loopPrice."</span>";
echo"<br><br>";
}
echo "</div>";
echo "<div id='cartCost'>$".$usdOwed."</div>";
?>
<form action="cart.php"><input type="submit" value="View Cart"></form>
</div>
<?php
$queryProducts ="SELECT * FROM products WHERE in_stock > 0 ORDER BY id ASC";
$resultH=mysqli_query($conn, $queryProducts) or die (mysqli_error($conn));
while($outputsH=mysqli_fetch_assoc($resultH)){
echo "<div class='shopCont'>";
echo "<div class='shopImg'><img src='".$outputsH['image']."'></div>";
echo "<div class='shopDesc'>";
echo "<span class='itemName'>".$outputsH['name']."</span>";
echo "<span class='itemCost'>$".$outputsH['price']."</span>";
echo $outputsH['description']."</div>";
echo "<div class='shopAdd'><form method='post'><input type='submit' value='Add To Cart' name='".$outputsH['id']."'></form></div>";
echo "</div>";
echo "<div class='shopCont'><hr></div>";
}
?>
<br>
</body>
</html>