forked from htmlacademy-php/264103-yeticave-12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_winner.php
45 lines (43 loc) · 1.69 KB
/
get_winner.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
<?php
require_once "init.php";
require_once "vendor/autoload.php";
require_once "helpers.php";
$lots_info = mysqli_query($con, "SELECT
lots.id AS lot_id,
lots.author_id,
lots.name AS lot_name,
users.email AS winner_email,
users.name AS winner_name,
users.id AS winner_id
FROM `lots` AS lots
JOIN `users` AS users
ON users.id = (SELECT `user_id` FROM `bids` WHERE lot_id = lots.id ORDER BY dt_add DESC LIMIT 1)
WHERE lots.end_date <= NOW()
AND lots.winner_id IS NULL");
for ($i = 1; $i <= mysqli_num_rows($lots_info); $i++) {
$lot = mysqli_fetch_assoc($lots_info);
$current_lot = $lot["lot_id"];
$lot_name = $lot["lot_name"];
$user_id_winner = $lot["winner_id"];
$update_winner = mysqli_query(
$con,
"UPDATE `lots` SET `winner_id` = " . $user_id_winner . " WHERE id = " . $current_lot
);
if ($update_winner) {
$transport = new Swift_SmtpTransport(MAIL["host"], MAIL["port"], MAIL["encryption"]);
$transport->setUsername(MAIL["username"]);
$transport->setPassword(MAIL["password"]);
$message = new Swift_Message("Ваша ставка победила");
$message->setTo([$lot["winner_email"] => $lot["winner_name"]]);
$msg_content = include_template("email.php", [
"user" => $lot["winner_name"],
"lot_id" => $current_lot,
"lot_name" => $lot_name,
"host_project" => $_SERVER["HTTP_HOST"],
]);
$message->setBody($msg_content, "text/html");
$mailer = new Swift_Mailer($transport);
$send_mail = $mailer->send($message);
}
}