-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
203 lines (176 loc) · 7.77 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
session_start();
try {
require_once 'db.php';
// a new products collection object
$collection = $db->products;
if (isset($_GET["search_cat"]) && isset($_GET["keyword"])) {
$keyword = $_GET["keyword"];
$value = $_GET["search_cat"];
$query = array($value => array('$regex' => new MongoRegex("/$keyword/i")));
$cursor = $collection->find($query);
} else {
// fetch all product documents
$cursor = $collection->find();
}
// How many results found
$num_docs = $cursor->count();
} catch (MongoConnectionException $e) {
// if there was an error, we catch and display the problem here
echo $e->getMessage();
} catch (MongoException $e) {
echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>S-Commerce - E-commerce platform</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="resources/css/style.css">
<script src="resources/js/script.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">S-Commerce</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="index.php">Home</a></li>
<?php if (isset($_SESSION["account"])) { ?>
<li class="active"><a href="account.php">Orders</a></li>
<?php } else { ?>
<li class="active"><a href="account.php">Account</a></li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php if (isset($_SESSION["account"])) { ?>
<li><a href="account.php?logout"><span class="glyphicon glyphicon-log-in"></span> Logout</a></li>
<?php } ?>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="well well-sm">
<strong>Display</strong>
<div class="btn-group">
<a href="#" id="list" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-th-list">
</span>List</a> <a href="#" id="grid" class="btn btn-default btn-sm"><span
class="glyphicon glyphicon-th"></span>Grid</a>
</div>
<a id="addToCart" class="btn btn-default btn-md" href="cart.php"><span
class="glyphicon glyphicon-shopping-cart btn-md"
id="basket"></span> products in
basket</a>
</div>
<?php if (!isset($_GET["id"])) { ?>
<div class="row">
<form action="index.php" method="get">
<div class="form-group pull-right">
<label for="sel1">Choose category</label>
<select name="search_cat" class="form-control" id="sel1">
<option value="title">Product name</option>
<option value="description">Product description</option>
</select>
<div class="input-group">
<input type="text" name="keyword" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div><!-- /input-group -->
</div>
</form>
</div>
<?php } else { ?>
<a onclick="window.history.back();" href="#" class="btn btn-primary btn-lg">
<span class="glyphicon glyphicon-arrow-left"></span> Back
</a>
<?php } ?>
<?php if (!isset($_GET["id"])) { ?>
<div id="products" class="row list-group">
<?php
if ($num_docs > 0) {
// loop over the results
foreach ($cursor as $obj) {
?>
<div class="item col-xs-4 col-lg-4">
<div class="thumbnail">
<img class="group list-group-image" src="uploads/<?php echo $obj['image']; ?>" alt=""/>
<div class="caption">
<h4 class="group inner list-group-item-heading"><a
href="index.php?id=<?php echo $obj['_id']; ?>"><?php echo $obj['title']; ?></a>
</h4>
<p id="description" class="group inner list-group-item-text">
<?php echo $obj['description']; ?></p>
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="lead">
$ <?php echo $obj['price']; ?></p>
</div>
<div class="col-xs-12 col-md-6">
<a class="btn btn-success"
onclick=addtocart('<?php echo $obj['_id'] . "','" . urlencode($obj['title']) . "','" . $obj["price"]; ?>')>Add
to cart</a>
</div>
</div>
</div>
</div>
</div>
<?php
}
} else {
// if no products are found, we show this message
echo "No products found \n";
}
?>
</div>
<?php } else {
$collection = new MongoCollection($db, 'products');
$sweetQuery = $collection->findOne(array('_id' => new MongoId($_GET["id"])));
$cursor = $collection->find($sweetQuery);
foreach ($cursor as $obj) {
?>
<div class="item">
<div class="thumbnail">
<img class="group list-group-image" src="uploads/<?php echo $obj['image']; ?>" alt=""/>
<div class="caption">
<h4 class="group inner list-group-item-heading"><a
href="index.php?id=<?php echo $obj['_id']; ?>"><?php echo $obj['title']; ?></a>
</h4>
<p class="group inner list-group-item-text">
<?php echo $obj['description']; ?></p>
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="lead">
$ <?php echo $obj['price']; ?></p>
</div>
<div class="col-xs-12 col-md-6">
<a class="btn btn-success"
onclick=addtocart('<?php echo $obj['_id'] . "','" . urlencode($obj['title']) . "','" . $obj["price"]; ?>')>Add
to cart</a>
</div>
</div>
</div>
</div>
</div>
<?php
}
}
// close the connection to MongoDB
$conn->close();
?>
</div>
</body>
</html>