-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlUsersOfficesData.php
116 lines (100 loc) · 3.23 KB
/
sqlUsersOfficesData.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
<?php
namespace Kommersant;
use PDO;
use PDOException;
/**
* Takes MySQL data from test database via PHP PDO and converts to string
*
* @method public __construct() creates connection to database via PDO and stores PDO instance to private property
* @method public usersOfficesToString() creates formatted string from database query of users and offices
* @method public officesWithMoreUsersToString() creates formatted string from database query of offices with more then 1 users
*/
class sqlUsersOfficesData
{
/**
* PDO instance
*/
protected object $pdo;
/**
* Creates PDO instance with class initialization
*/
public function __construct()
{
// can takes connect parameters from config if you wish
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '32143214wp';
$charset = 'utf8';
// create dsn string and add useful options
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
// try to setup connections
try
{
$this->pdo = new PDO($dsn, $user, $pass, $opt);
}
catch (PDOException $err)
{
die("Подключение к базе данных не удалось: {$err->getMessage()}");
}
}
/**
* Makes select query to MySQL via class PDO instance and creates string with info of users and offices
*
* @return $usersOfficeDisplayString formated string, use to display data via echo output
*/
public function usersOfficesToString()
{
$usersOfficeArray = $this->pdo->query(
"SELECT
users.name AS 'UserName',
offices.name AS 'OfficeName'
FROM
users
JOIN offices ON users.office_id = offices.id
ORDER BY users.name ASC"
)->fetchAll();
// string with header to concat
$usersOfficeDisplayString = 'Имена пользователей и названия офисов, в которых они сидят:<br />';
foreach( $usersOfficeArray as $key => $value )
{
$usersOfficeDisplayString .= "Имя пользователя: {$value['UserName']}, Название офиса: {$value['OfficeName']}<br />";
}
return $usersOfficeDisplayString;
}
/**
* Makes select query to MySQL via class PDO instance and creates string with info of offices with more then 1 users
*
* @return $officesDisplayString formated string, use to display data via echo output
*/
public function officesWithMoreUsersToString()
{
$officesArray = $this->pdo->query(
"SELECT
offices.name AS 'OfficeName'
FROM
users
JOIN offices ON users.office_id = offices.id
GROUP BY offices.name
HAVING COUNT(offices.name) >= 2
ORDER BY offices.name ASC;"
)->fetchAll();
// string with header to concat
$officesDisplayString = 'Офисы с больше чем одним пользователем:<br />';
foreach( $officesArray as $key => $value )
{
$officesDisplayString .= "Название офиса: {$value['OfficeName']}<br />";
}
return $officesDisplayString;
}
}
// example use
$sqlUsersOfficesData = new sqlUsersOfficesData;
echo( $sqlUsersOfficesData->usersOfficesToString() );
echo( '<br />' );
echo( $sqlUsersOfficesData->officesWithMoreUsersToString() );