-
Notifications
You must be signed in to change notification settings - Fork 0
/
auc.php
396 lines (326 loc) · 11.7 KB
/
auc.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
session_start();
date_default_timezone_set('Asia/Kolkata');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if the user is logged in
if (isset($_SESSION['username'])) {
// Get the submitted bid details
$item_name = $_POST['item_id'];
$bidder_name = $_SESSION['username'];
$bid_amount = $_POST['bid_amount'];
// Connect to the database
$host = 'localhost';
$user = 'root';
$password = ''; // Replace with your actual database password
$database = 'project'; // Replace with your actual database name
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Retrieve the current highest bid for the item
$current_bid_query = "SELECT MAX(bid_amount) AS current_bid FROM bids WHERE item_id='$item_name'";
$current_bid_result = mysqli_query($conn, $current_bid_query);
$current_bid_row = mysqli_fetch_assoc($current_bid_result);
$current_bid = $current_bid_row['current_bid'];
$starting_bid_query = "SELECT starting_bid FROM items WHERE item_id='$item_name'";
$starting_bid_result = mysqli_query($conn, $starting_bid_query);
$starting_bid_row = mysqli_fetch_assoc($starting_bid_result);
$starting_bid = $starting_bid_row['starting_bid'];
$winning_bid_query= "SELECT b.bid_amount AS max_bid,b.bidder_name FROM bids b WHERE b.item_id = '$item_name' AND b.bid_amount = (SELECT MAX(bid_amount) FROM bids WHERE item_id = '$item_name')";
$winning_bid_result = mysqli_query($conn, $winning_bid_query);
if ($winning_bid_result && mysqli_num_rows($winning_bid_result) > 0) {
$winning_bid_row = mysqli_fetch_assoc($winning_bid_result);
$winning_bid = $winning_bid_row['max_bid'];
$winning_bidder = $winning_bid_row['bidder_name'];
} else {
$winning_bid = null;
$winning_bidder = null;
}
$auction_end_time='2023-07-13 11:15:00';
$current_time = time();
$end_time = strtotime($auction_end_time);
if ($current_time >= $end_time) {
echo "<p>The auction has ended. No further bids are accepted.</p>";
} else {
// Check if the submitted bid is higher than the current highest bid or the starting price
if ($bid_amount > $current_bid && $bid_amount > $starting_bid) {
// Insert the new bid into the database
$insert_query = "INSERT INTO bids (item_id, bidder_name, bid_amount) VALUES ('$item_name', '$bidder_name', '$bid_amount')";
if (mysqli_query($conn, $insert_query)) {
echo "<p>Your bid of ₹$bid_amount for item_id:$item_name has been successfully placed.</p>";
} else {
echo "<p>Failed to place the bid. Please try again.</p>";
}
} else {
echo "<p>Your bid of ₹$bid_amount for item_id:$item_name is not higher than the current highest bid.</p>";
}
}
// Close the database connection
mysqli_close($conn);
} else {
echo "<p>Please log in to place a bid.</p>";
}
} else {
echo "<p>Invalid request.</p>";
}
?>
<?php
// AUCTION ITEM CLASS
class AuctionItem {
public $id;
public $name;
public $description;
public $starting_bid;
public $current_bid;
public $bids;
public $end_time;
public function __construct($id, $name, $starting_bid) {
$this->id = $id; // Assign the item ID
$this->name = $name;
$this->current_bid = $starting_bid;
$this->bids = [];
$this->end_time = '2023-07-13 11:15:00';
}
public function is_auction_ended() {
$current_time = time();
$end_time = strtotime($this->end_time);
return $current_time > $end_time;
}
public function getClosingTime() {
return $this->end_time;
}
public function __toString() {
return "$this->name Auction\nStarting Bid: ₹$this->starting_bid\nEnd Time: $this->end_time\nDescription: $this->description";
}
public function place_bid($bidder_name, $bid_amount) {
if ($bid_amount > $this->current_bid) {
$this->current_bid = $bid_amount;
$this->bids[] = [
'bidder_name' => $bidder_name,
'bid_amount' => $bid_amount
];
return true;
} else {
return false;
}
}
public function getBids() {
return $this->bids;
}public function getWinningBid() {
return $this->winning_bid;
}
}
// AUCTION CLASS
class Auction {
public $items;
public $bidders;
public function __construct() {
$this->items = [];
$this->bidders = [];
}
public function add_item($item) {
$this->items[] = $item;
}
public function register_bidder($bidder_name) {
$this->bidders[$bidder_name] = 1000; // Each bidder starts with $1000
}
public function insert_winner_details($item_name, $winning_bid, $winning_bidder) {
$host = 'localhost';
$user = 'root';
$password = ''; // Replace with your actual database password
$database = 'project'; // Replace with your actual database name
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$insert_winner_query = "INSERT INTO winners (item_id, bidder_name, bid_amount) VALUES ('$item_name', '$winning_bidder', '$winning_bid')";
mysqli_query($conn, $insert_winner_query);
// Close the database connection
mysqli_close($conn);
}
public function find_item_by_name($name) {
// Connect to the database
$conn = mysqli_connect('localhost', 'root', '', 'project');
// Prepare and execute the query to retrieve the item by name
$query = "SELECT * FROM items WHERE item_id = '$name'";
$result = mysqli_query($conn, $query);
// Check if the item was found
if (mysqli_num_rows($result) > 0) {
// Fetch the item data from the result set
$row = mysqli_fetch_assoc($result);
// Create an AuctionItem object and populate its properties
$item = new AuctionItem(
$row['item_id'],
$row['item_name'],
$row['starting_bid']
);
// Close the database connection
mysqli_close($conn);
// Return the item object
return $item;
} else {
// Item not found
return null;
}
}
public function start_auction($item_name, $bidder_name, $bid_amount) {
$item = $this->find_item_by_name($item_name);
if ($item) {
if (!array_key_exists($bidder_name, $this->bidders)) {
echo "You need to register first!";
return;
}
if ($item->is_auction_ended()) {
echo "<br>Auction closed!<br>";
$host = 'localhost';
$user = 'root';
$password = ''; // Replace with your actual database password
$database = 'project'; // Replace with your actual database name
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$winning_bid_query ="SELECT b.bid_amount AS max_bid,b.bidder_name FROM bids b WHERE b.item_id = '$item_name' AND b.bid_amount = (SELECT MAX(bid_amount) FROM bids WHERE item_id = '$item_name')";
$winning_bid_result = mysqli_query($conn, $winning_bid_query);
if ($winning_bid_result && mysqli_num_rows($winning_bid_result) > 0) {
$winning_bid_row = mysqli_fetch_assoc($winning_bid_result);
$winning_bid = $winning_bid_row['max_bid'];
$winning_bidder = $winning_bid_row['bidder_name'];
} else {
$winning_bid = null;
$winning_bidder = null;
}
$bids_query = "SELECT bid_amount, bidder_name FROM bids WHERE item_id = '$item_name'";
$bids_result = mysqli_query($conn, $bids_query);
echo "winning bid: ₹".$winning_bid;
if (mysqli_num_rows($bids_result) > 0) {
echo "<br>Bids:";
while ($bid_row = mysqli_fetch_assoc($bids_result)) {
$bid_amount = $bid_row['bid_amount'];
$bidder_name = $bid_row['bidder_name'];
echo "<br>Bidder: $bidder_name, Amount: ₹$bid_amount";
}
}
$winner_details_query = "SELECT * FROM winners WHERE item_id = '$item_name'";
$winner_details_result = mysqli_query($conn, $winner_details_query);
if (mysqli_num_rows($winner_details_result) == 0) {
// Insert the winner details
$this->insert_winner_details($item_name, $winning_bid, $winning_bidder);
}
if ($winning_bid) {
echo "<br>Winner: " . $winning_bid_row['bidder_name'] ;
echo "\nWinning Price: ₹" . $winning_bid;
}
else {
echo "\nNo bids were placed for this item.";
}
} else {
if ($bid_amount <= 0) {
echo "Invalid input! Please enter a positive number";
return;
}
if ($item->place_bid($bidder_name, $bid_amount) ) {
echo "Bid accepted!";
} else {
echo "Your bid is not high enough!";
}
echo "<br>Auction is still ongoing.";
}
} else {
echo "Item not found!";
}
echo "<br>Current Time: " . date('Y-m-d H:i:s');
if ($item && $item->is_auction_ended()) {
// Perform any necessary actions after the auction ends
}
}
}
// MAIN CODE
$auction = new Auction();
// Add items to the auction
$item1 = new AuctionItem(1, "Guitar", "A Fender Stratocaster guitar", 5000);
$item2 = new AuctionItem(2, "Oppo Phone", "A brand new Oppo phone", 15000);
$item3 = new AuctionItem(3, "Paintings", "A collection of beautiful paintings", 25000);
$item4 = new AuctionItem(4, "Jersey", "Indian Cricket team all format jerseys", 100000);
$auction->add_item($item1);
$auction->add_item($item2);
$auction->add_item($item3);
$auction->add_item($item4);
// Register bidders
$auction->register_bidder($_SESSION['username']);
// Start the auction
$item_name = $_POST['item_id'];
$bidder_name = $_SESSION['username'];
$bid_amount = $_POST['bid_amount'];
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<title>Auction Result</title>";
echo "<style>";
// CSS styles for the result page
echo "body {";
echo " font-family: Arial, sans-serif;";
echo " margin: 0;";
echo " padding: 0;";
echo " background-color: green;";
echo "font-size:24px;";
echo "}";
echo "header {";
echo " background-color:green;";
echo " padding: 20px;";
echo " text-align: center;";
echo "}";
echo "h1 {";
echo " margin: 0;";
echo " color: #333;";
echo " font-size: 30px;";
echo "}";
echo "main {";
echo " margin: 20px;";
echo " background-color:green;";
echo " text-align: center;";
echo " font-size: 24px;";
echo "}";
echo ".center {";
echo " display: flex;";
echo " flex-direction: row;";
echo " justify-content: center;";
echo " margin-top: 20px;";
echo "}";
echo "button {";
echo " background-color: #007bff;";
echo " color: #fff;";
echo " border: none;";
echo " padding: 10px 20px;";
echo " margin: 5px;";
echo " border-radius: 5px;";
echo " cursor: pointer;";
echo "}";
echo "button a {";
echo " color: #fff;";
echo " text-decoration: none;";
echo "}";
echo "</style>";
echo "</head>";
echo "<body>";
echo "<header>";
echo "<h1>Auction Result</h1>";
echo "</header>";
echo "<main>";
$auction->start_auction($item_name, $bidder_name, $bid_amount);
// Retrieve the item from the auction
$item = $auction->find_item_by_name($item_name);
if ($item) {
$closing_time = $item->getClosingTime();
echo "<p>Auction will be closed on: $closing_time</p>";
}
echo "</main>";
echo "<div class='center'>";
echo "<div class='button'>";
echo "<button><a href='home.php'>Back to Home</a></button>";
echo "<button><a href='categories.php'>Back to Buy Items</a></button>";
echo "</div>";
echo "</div>";
echo "</body>";
echo "</html>";
?>