-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
103 lines (94 loc) · 3.39 KB
/
functions.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
<?php
function dbConnect(): PDO
{
$db = new PDO('mysql:host=db;dbname=collection_app', 'root', 'password');
$db->setAttribute(PDO:: ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $db;
}
function getData(PDO $db):array
{
$query = $db->prepare('SELECT `name`, `painting_title`, `type`, `description`, `type` , `image`
FROM `painting` JOIN `artist` ON `painting`.`artist_name` = `artist`.`id`;');
$query->execute();
$result = $query->fetchAll();
return $result;
}
function extractDataAndDisplay(array $data):string
{
$text = '<section class="container-box">';
foreach ($data as $row) {
$title = $row['painting_title'];
$name = $row['name'];
$type = $row['type'];
$description = $row['description'];
$image = $row['image'];
$text .= '<div class="sub-box">'
. '<img src="img/'. $image . '" alt="'. $image .'">'
. '<h2 class="title">' . $title . '</h2>'
. '<h4 class="artist">' . "Artist: " . $name . '</h4>'
. '<h5 class="type">' . "Type: " .$type . '</h5>'
. '<p class="descripton">' . "Description: </p>" . '<p>' . $description . '</p>' .
'</div>';
}
$text .= '</section>';
return $text;
}
function executeFormCheckAndDataInsert(array $get): string
{
$message = '';
if (empty($get)) {
$message = '' ;
} else {
$title = $get['title'];
$artist_name = $get['artist_name'];
$type = $get['type'];
$description = $get['description'];
$message .= isFormCorrect($title, $artist_name, $type, $description);
if ($message === '') {
$db = dbConnect();
$message .= isInDataBase($db,$title);
if ($message === '') {
$message .= insertDataIntoDataBase($db, $title, $artist_name, $type, $description);
}
}
}
return $message;
}
function isFormCorrect(string $title, string $artist_name, string $type, string $description): string
{
if (empty($title) || empty($type) || empty($description) || ($artist_name == '0')) {
return '<div class="message">Please make sure to fill all the field in the form. Thanks you</div>';
} else {
return '';
}
}
function isInDataBase(PDO $db, string $title): string
{
$query = $db->prepare('SELECT `painting_title` FROM `painting` WHERE `painting_title` = :title ');
$query->execute(
[
':title' => $title,
]
);
$result = $query->fetch();
if ($result == true) {
return '<div class="message">' .
'You already Own that paint in your gallery, be nice and give this copy to a commoner' . '</div>';
} else {
return '';
}
}
function insertDataIntoDataBase(PDO $db, string $title, string $artist_name, string $type, string $description, string $image = "no-image.png"): string
{
$query = $db->prepare('INSERT INTO `painting` (`painting_title`, `artist_name` ,`type`, `description`, `image` ) VALUES (:title, :artist_name, :type, :description, :image);');
$query->execute(
[
':title' => $title,
':artist_name' => $artist_name,
':type' => $type,
':description' => $description,
':image' => $image,
]
);
return '<div class="message">The new painting has been move into the gallery</div>';
}