forked from pilohem/crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.php
136 lines (128 loc) · 4.5 KB
/
util.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
<?php
function flashMessages() {
if ( isset($_SESSION['error']) ) {
echo '<p style="color:red">'.$_SESSION['error']."</p>\n";
unset($_SESSION['error']);
}
if ( isset($_SESSION['success']) ) {
echo '<p style="color:green">'.$_SESSION['success']."</p>\n";
unset($_SESSION['success']);
}
}
function validateProfile() {
if ( strlen($_POST['first_name']) < 1 || strlen($_POST['last_name']) < 1
|| strlen($_POST['email']) < 1 || strlen($_POST['headline']) < 1
|| strlen($_POST['summary']) < 1) {
return "All fields are required";
}
//check if username has an at-sign @
if (strpos($_POST['email'], '@') === false) {
return "Email must have an at-sign (@)";
}
return true;
}
function validatePos() {
for($i=1; $i<=9; $i++) {
if ( ! isset($_POST['year'.$i]) ) continue;
if ( ! isset($_POST['desc'.$i]) ) continue;
$year = $_POST['year'.$i];
$desc = $_POST['desc'.$i];
if ( strlen($year) == 0 || strlen($desc) == 0 ) {
return "All fields are required";
}
if ( ! is_numeric($year) ) {
return "Position year must be numeric";
}
}
return true;
}
function validateEdu() {
for($i=1; $i<=9; $i++) {
if ( ! isset($_POST['edu_year'.$i]) ) continue;
if ( ! isset($_POST['edu_school'.$i]) ) continue;
$edu_year = $_POST['edu_year'.$i];
$school = $_POST['edu_school'.$i];
if ( strlen($edu_year) == 0 || strlen($school) == 0 ) {
return "All fields are required";
}
if ( ! is_numeric($edu_year) ) {
return "Education year must be numeric";
}
}
return true;
}
function insertPositions($pdo, $profile_id) {
// Insert the position entries
$rank = 1;
for($i=1; $i<=9; $i++) {
if ( ! isset($_POST['year'.$i]) ) continue;
if ( ! isset($_POST['desc'.$i]) ) continue;
$year = $_POST['year'.$i];
$desc = $_POST['desc'.$i];
$stmt = $pdo->prepare('INSERT INTO Position
(profile_id, rank, year, description)
VALUES ( :pid, :rank, :year, :desc)');
$stmt->execute(array(
':pid' => $profile_id,
':rank' => $rank,
':year' => $year,
':desc' => $desc)
);
$rank++;
}
}
function insertEducations($pdo, $profile_id) {
// Insert the position entries
$rank = 1;
for($i=1; $i<=9; $i++) {
if ( ! isset($_POST['edu_year'.$i]) ) continue;
if ( ! isset($_POST['edu_school'.$i]) ) continue;
$edu_year = $_POST['edu_year'.$i];
$school = $_POST['edu_school'.$i];
//Check if there is already a school with that name
$stmt = $pdo->prepare('SELECT * FROM Institution WHERE name = :nm');
$stmt->execute(array(
':nm' => $school)
);
$institutions = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($institutions) === 0) {
//The school is NOT in the database
//Insert the name of the school in the Institution table
$stmt = $pdo->prepare('INSERT INTO Institution (name) VALUES (:nm)');
$stmt->execute(array(
':nm' => $school)
);
//Select the NEW entries name and institution_id from the Institution table
$stmt = $pdo->prepare('SELECT * FROM Institution WHERE name = :nm');
$stmt->execute(array(
':nm' => $school)
);
$institutions = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//Insert entry into the Education table
$stmt = $pdo->prepare('INSERT INTO Education
(profile_id, institution_id, rank, year) VALUES ( :pid, :school_id, :rank, :year)');
$stmt->execute(array(
':pid' => $profile_id,
':school_id' => $institutions[0]['institution_id'],
':rank' => $rank,
':year' => $edu_year)
);
$rank++;
}
}
function loadPos($pdo, $profile_id) {
$stmt = $pdo->prepare("SELECT * FROM Position
WHERE profile_id = :profile_id ORDER BY rank ASC");
$stmt->execute(array(':profile_id' => $profile_id));
$positions = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $positions;
}
function loadEdu($pdo, $profile_id) {
$stmt = $pdo->prepare("SELECT year, name FROM Education JOIN Institution
ON Education.institution_id = Institution.institution_id
WHERE profile_id = :profile_id ORDER BY rank");
$stmt->execute(array( ':profile_id' => $profile_id));
$educations = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $educations;
}