-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserpage.php
More file actions
202 lines (188 loc) · 8.96 KB
/
userpage.php
File metadata and controls
202 lines (188 loc) · 8.96 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
session_start();
include('header.php');
?>
<head>
<link rel="stylesheet" type="text/css" href="css\ProfileStyle.css">
<link rel="stylesheet" type="text/css" href="css\BlogStyle.css">
</head>
<body>
<?php
require 'includes/dbh.inc.php';
include 'distance.php';
$id = $_GET['IdUser'];
if (isset($_SESSION['userId'])) {
// Retrieve postal code from current user
$currentUserId = $_SESSION['userId'];
$sql = "SELECT postalCode FROM User WHERE idUser = $currentUserId";
$statement = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($statement, $sql)) {
header("Location: userpage?error=sqlerror");
echo '<div class="newposterror"><p>Er is iets fout gegaan (sql error).</p></div>';
}
else {
mysqli_stmt_execute($statement);
$result = mysqli_stmt_get_result($statement);
if ($row = mysqli_fetch_assoc($result)) {
$currentUserPostalCode = $row['postalCode'];
}
}
}
$sql = $conn->query("SELECT * FROM User u WHERE idUser='$id'") or die($conn->error);
/* fetch associative array */
while ($row = $sql->fetch_assoc()) {
$gebruikersnaam = $row["usernameUser"];
$biography = $row["biography"];
$_SESSION["idUser"] = $row["idUser"];
}
//count total advertisements of user
$sql = $conn->query("SELECT COUNT(a.idAd) as totalAds FROM User u JOIN Advertisement a ON a.userId = u.idUser WHERE idUser='$id'") or die($conn->error);
while ($row = $sql->fetch_assoc()) {
$countAds = $row["totalAds"];
}
//count total blogposts of user
$sql = $conn->query("SELECT COUNT(b.idPost) as totalBlogs FROM User u JOIN Blogpost b ON b.blogUserId = u.idUser WHERE idUser='$id'") or die($conn->error);
while ($row = $sql->fetch_assoc()) {
$countBlogs = $row["totalBlogs"];
}
?>
<table class="profilebox">
<tr>
<div class="profilebox">
<h1><?php echo $gebruikersnaam; ?>'s profiel</h1>
</div>
</tr>
<tr>
<div class="userprofilebox">
<div class="userprofile-up-left">
<p>Beoordeling</p>
<?php
$sql = "SELECT * FROM Rating WHERE adUser = '$id'";
$result = $conn->query($sql);
$allUserRatingRates = array();
foreach($result as $ratingId) {
// output data of each row
array_push($allUserRatingRates, $ratingId['userRating']);
}
$averageUserRating = array_sum($allUserRatingRates) / count($allUserRatingRates);
if(count($allUserRatingRates) == 0){
$averageUserRating = 0;
}
for($i = 0; $i < $averageUserRating; $i++){
echo '<label class="fa fa-star star-checked"></label>';
}
for($i = 4; $i >= $averageUserRating; $i--){
echo '<label class="fa fa-star"></label>';
}
echo "<h3 style='display: inline;'>(".count($allUserRatingRates).")</h3>";
?>
<p>Aantal advertenties</p>
<p class="userData"><?php echo $countAds; ?> advertenties</p>
<p>Aantal blogposts</p>
<p class="userData"><?php echo $countBlogs; ?> blogposts</p>
</div>
<div class="userprofile-up-right">
<p>Gebruikersnaam</p>
<p class="userData"><?php echo $gebruikersnaam; ?></p>
<p>Biografie</p>
<p class="userData"><?php echo $biography != "" ? $biography : "Gebruiker heeft nog geen biografie toegevoegd." ; ?></p>
</div>
</div>
</tr>
</table>
<!-- User advertisements and blogposts switching tabs -->
<div class="ads-blogs-box">
<div class="ads-blogs-tabs">
<div id="ads-blogs-btns"></div>
<button id='ad-btn' class='ads-blogs-toggle-btn' onclick="leftClick()"><?php echo $gebruikersnaam; ?>'s advertenties</button>
<button id='blog-btn' class='ads-blogs-toggle-btn' onclick="rightClick()"><?php echo $gebruikersnaam; ?>'s blogposts</button>
</div>
</div>
<div id="userAdsList">
<div class="img-area">
<?php
$sql = "SELECT * FROM Advertisement a JOIN User u ON a.userId = u.idUser JOIN AdImage ai ON a.idAd = ai.idAdvert WHERE a.userId = '$id' ORDER BY a.idAd DESC";
$result = $conn->query($sql);
//array with all advertisement Ids
//array used to avoid showing the same advertisement more than once with a different image
$allIdAdvertisements = array();
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//checks if advertisement id already exists in array > if advertisement id exists in array -> skip current advertisement
if(!in_array($row['idAd'], $allIdAdvertisements)){
if (isset($_SESSION['userId'])) {
$distance = getDistance($row['postalCode'], $currentUserPostalCode);
} else {
$distance = "-- km";
}
echo '<div class="plant">
<a class="linkPlant" href="adinfo?idAd='.$row['idAd'].'">
<div class="adImage">
<img src="uploads/'.$row["imgName"].'" alt="">
</div>
<div class="description">
<h2>'.$row['plantName'].'</h2>
<br>
<h3> Afstand: <span>'.$distance.'</span></h3>
<h3> Datum: <span>'.date("d-m-Y", strtotime($row['postDate'])).'</span></h3>
</div>
</a>
</div>';
//add advertisement id to array
array_push($allIdAdvertisements, $row['idAd']);
}
}
} else {
echo "<p class='emptyAdOrBlogList'>Gebruiker heeft nog geen advertenties geplaatst.</p>";
}
?>
</div>
</div>
<div id="userBlogsList">
<div class="grid-3-col">
<?php
$sql = "SELECT * FROM Blogpost b JOIN User u ON b.blogUserId = u.idUser LEFT JOIN BlogImage bi ON b.idPost = bi.idBlog WHERE b.blogUserId = '$id' ORDER BY b.idPost DESC";
$result = $conn->query($sql);
$number_of_posts = $result->num_rows;
//array with all blogpost Ids
$allIdPosts = array();
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//checks if blogpost id already exists in array > if blogpost id exists in array -> skip current blogpost
if(!in_array($row['idPost'], $allIdPosts)){
if(empty($row["imgName"])){
$blogImage = 'images/plantje.png';
} else {
$blogImage = 'uploads/'.$row['imgName'];
}
echo '<div class="blogpost">
<a class="linkPlant" href="bloginfo?idBlog='.$row["idPost"].'">
<div class="blogImage">
<img src="'.$blogImage.'" alt="">
</div>
<div class="blogDescription">
<h2>'.$row["blogTitle"].'</h2>
<h3>'.$row["firstName"].'</h3>
<p>'.$row["blogDesc"].'</p>
<h4 class="alignleft">'.date_format(date_create($row["blogDate"]),"d-m-Y").'</h4>
<h4 class="alignright">'.$row["blogCategory"].'</h4>
</div>
</a>
</div>';
//add blogpost id to array
array_push($allIdPosts, $row['idPost']);
}
}
} else {
echo "<p class='emptyAdOrBlogList'>Gebruiker heeft nog geen blogposts geplaatst.</p>";
}
?>
</div>
</div>
</body>
<?php
include('footer.php');
include('feedback.php');
?>