Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module4 task2 #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ Thumbs.db
vendor
uploads/*
!uploads/.gitkeep
info.php
adminer.php
2 changes: 0 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,3 @@
);

print ($layout_content);

?>
18 changes: 17 additions & 1 deletion myfunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function translate_price($item)
/**
* Count how much time exist before expiry date of this item
*
* @param array $item Array of concrete which contains this date
* @param array $item Array of concrete item which contains this date
*
* @return int how much time exist
*/
Expand All @@ -26,3 +26,19 @@ function count_time($item)
$minutes = floor(($diff - $hours * 3600) / 60);
return $hours . ':' . $minutes;
}

/**
* Show so close expiry date from current moment
*
* @param array $item Array of concrete item which contains this date
*
* @return bool true when there is 1 hour left before the expiry date
* and false when there is more than 1 hour left before
*/
function is_expired($item)
{
if ((strtotime($item['expiry_date']) - strtotime(date('Y-m-d'))) <= 3600) {
return true;
}
return false;
}
129 changes: 129 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* Create the table of categories with current categories */
INSERT INTO categories (name, eng_name) VALUES
('Доски и лыжи', 'boards'),
('Крепления', 'attachment'),
('Ботинки', 'boots'),
('Одежда', 'clothing'),
('Инструменты', 'tools'),
('Разное', 'other');

/* Create the table of users */
INSERT INTO users (email, name, password, contacts) VALUES
('[email protected]', 'keks', '12578g', 'https://tlgg.ru/keks'),
('[email protected]', 'your_cat', 'gav057', 'https://tlgg.ru/your_cat'),
('[email protected]', 'kot_ik', '3pv007', 'https://tlgg.ru/kot_ik');

/* Create the table of items with current items */
INSERT INTO items (name, description, image, first_price, expiry_date, step_bet, author_id, winner_id, category_id) VALUES
(
'2014 Rossignol District Snowboard',
NULL,
'img/lot-1.jpg',
10999,
'2022-05-22',
500,
(SELECT id FROM users WHERE name = 'keks'),
NULL,
(SELECT id FROM categories WHERE eng_name = 'boards')
),
(
'DC Ply Mens 2016/2017 Snowboard',
NULL,
'img/lot-2.jpg',
159999,
'2022-05-23',
1500,
(SELECT id FROM users WHERE name = 'your_cat'),
NULL,
(SELECT id FROM categories WHERE eng_name = 'boards')
),
(
'Крепления Union Contact Pro 2015 года размер L/XL',
NULL,
'img/lot-3.jpg',
8000,
'2022-05-24',
500,
(SELECT id FROM users WHERE name = 'keks'),
NULL,
(SELECT id FROM categories WHERE eng_name = 'attachment')
),
(
'Ботинки для сноуборда DC Mutiny Charocal',
NULL,
'img/lot-4.jpg',
10999,
'2022-05-25',
500,
(SELECT id FROM users WHERE name = 'kot_ik'),
NULL,
(SELECT id FROM categories WHERE eng_name = 'boots')
),
(
'Куртка для сноуборда DC Mutiny Charocal',
NULL,
'img/lot-5.jpg',
7500,
'2022-05-26',
500,
(SELECT id FROM users WHERE name = 'kot_ik'),
NULL,
(SELECT id FROM categories WHERE eng_name = 'clothing')
),
(
'Маска Oakley Canopy',
NULL,
'img/lot-6.jpg',
5400,
'2022-05-27',
200,
(SELECT id FROM users WHERE name = 'your_cat'),
NULL,
(SELECT id FROM categories WHERE eng_name = 'other')
);

/* Create the table of bets */
INSERT INTO bets (price, user_id, item_id) VALUES
(
6000,
(SELECT id FROM users WHERE name = 'kot_ik'),
(SELECT id FROM items WHERE name = 'Маска Oakley Canopy')
),
(
5800,
(SELECT id FROM users WHERE name = 'keks'),
(SELECT id FROM items WHERE name = 'Маска Oakley Canopy')
),
(
9000,
(SELECT id FROM users WHERE name = 'your_cat'),
(SELECT id FROM items WHERE name = 'Крепления Union Contact Pro 2015 года размер L/XL')
);

/* Take all categories */
SELECT * FROM categories;

/* Take the newest and opened items */
SELECT i.name, first_price, image, price, c.name FROM items i
JOIN bets b ON b.item_id = i.id
JOIN categories c ON i.category_id = c.id
WHERE expiry_date > CURRENT_TIMESTAMP
ORDER BY expiry_date DESC;

/* Take items by its ID */
SELECT i.name, description, image, first_price, expiry_date, step_bet, u.name, c.name
FROM items i
JOIN users u ON i.user_id = u.id
JOIN categories c ON i.category_id = c.id
WHERE i.id = 2;

/* Update item_name by its ID */
UPDATE items SET name = 'Крепления Union Contact Pro 2015 года размер S/M'
WHERE id = 3;

/* Take the list of bets for the item */
SELECT i.name, price, u.name FROM bets b
JOIN items i ON b.item_id = i.id
JOIN users u ON b.user_id = u.id
WHERE i.id = 6
ORDER BY b.dt_add ASC;
52 changes: 52 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
DROP DATABASE IF EXISTS yeticave;

CREATE DATABASE yeticave
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;

USE yeticave;

CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt_add TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
email VARCHAR(128) NOT NULL UNIQUE,
name VARCHAR(255),
password CHAR(255) NOT NULL,
contacts VARCHAR(255)
);

CREATE TABLE categories (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
eng_name VARCHAR(255)
);

CREATE TABLE items (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt_add TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name VARCHAR(255) NOT NULL,
description TEXT,
image VARCHAR(255) DEFAULT NULL,
first_price INT NOT NULL,
expiry_date TIMESTAMP NOT NULL,
step_bet INT NOT NULL,
author_id INT,
FOREIGN KEY (author_id) REFERENCES users(id),
winner_id INT,
FOREIGN KEY (winner_id) REFERENCES users(id),
category_id INT,
FOREIGN KEY (category_id) REFERENCES categories(id)
);

CREATE TABLE bets (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt_add TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
price INT NOT NULL,
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(id),
item_id INT,
FOREIGN KEY (item_id) REFERENCES items(id)
);

CREATE INDEX c_name ON categories(name);
CREATE INDEX i_name ON items(name);
2 changes: 1 addition & 1 deletion templates/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<span class="lot__cost"><?= translate_price($item); ?></span>
</div>
<div class="lot__timer timer
<?php if ((strtotime($item['expiry_date']) - strtotime(date('Y-m-d'))) <= 3600) : ?>
<?php if (is_expired($item)) : ?>
timer--finishing
<?php endif; ?>
">
Expand Down