-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuserProfile.php~
265 lines (208 loc) · 9.33 KB
/
userProfile.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
session_start();
include 'mylib.php'; // Include your database connection file
db_connect(); // Establish the database connection
global $db;
// Check if the user is logged in
if (!isset($_SESSION['email'])) {
header("Location: login.php");
exit();
}
// Get the display name from the URL (e.g., /displayName/userName)
$profileDisplayName = $_GET['displayName']; // Ensure to sanitize and validate this input
// Fetch the user's profile data based on display name
$sql = "SELECT UserID, profilePicture, bio, igLink, tiktokLink, tumblrLink, twitterLink, fbLink, personalLink
FROM users
WHERE displayName = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $profileDisplayName);
$stmt->execute();
$stmt->bind_result($profileUserID, $profilePicture, $bio, $igLink, $tiktokLink, $tumblrLink, $twitterLink, $fbLink, $personalLink);
$stmt->fetch();
$stmt->close();
if (!$profileUserID) {
echo "User not found.";
exit();
}
// Check if the logged-in user is viewing their own profile
$isOwnProfile = ($loggedInUserID == $profileUserID);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="https://fashion-finder.store/images/favicon.ico">
<link href="https://fonts.googleapis.com/css2?family=Chakra+Petch:wght@300;400;500;600;700&display=swap" rel="stylesheet">
</head>
<body style="color:#fff;">
<?php include 'elements/header.php'; ?>
<div class="post-header">
<h1>
<!-- Display the profile picture of the user being viewed next to their display name -->
<?php if (!empty($profilePicture)): ?>
<img src="<?php echo htmlspecialchars($profilePicture); ?>" alt="Profile Picture" style="width:50px; height:50px; border-radius:50%; margin-right:10px;">
<?php endif; ?>
<?php echo htmlspecialchars($profileDisplayName);?>
<?php if (!$isOwnProfile): ?>
<?php
// Check if the logged-in user is already following this profile user
$isFollowing = false;
$checkFollowSql = "SELECT * FROM FollowingConnections WHERE UserID = ? AND FollowingID = ?";
$checkFollowStmt = $db->prepare($checkFollowSql);
$checkFollowStmt->bind_param("ii", $loggedInUserID, $profileUserID);
$checkFollowStmt->execute();
$isFollowing = $checkFollowStmt->get_result()->num_rows > 0;
$checkFollowStmt->close();
?>
<!-- Follow/Unfollow Button -->
<button class="follow-button" id="followButton" data-userid="<?php echo $profileUserID; ?>" data-action="<?php echo $isFollowing ? 'unfollow' : 'follow'; ?>">
<?php echo $isFollowing ? 'Unfollow' : 'Follow'; ?>
</button>
<?php endif; ?>
</h1>
</div>
<div class="follow-stats">
<?php
// Follower and following count
$stmt = $db->prepare("SELECT COUNT(*) AS followerCount FROM FollowingConnections WHERE FollowingID = ?");
$stmt->bind_param("i", $profileUserID);
$stmt->execute();
$stmt->bind_result($followerCount);
$stmt->fetch();
$stmt->close();
$stmt = $db->prepare("SELECT COUNT(*) AS followingCount FROM FollowingConnections WHERE UserID = ?");
$stmt->bind_param("i", $profileUserID);
$stmt->execute();
$stmt->bind_result($followingCount);
$stmt->fetch();
$stmt->close();
//echo "Followers: " . $followerCount . " | Following: " . $followingCount;
?>
</div>
<a href="javascript:void(0);" onclick="showFollowers(<?php echo $profileUserID; ?>)">
Followers: <?php echo $followerCount; ?>
</a>
<a href="javascript:void(0);" onclick="showFollowing(<?php echo $profileUserID; ?>)">
Following: <?php echo $followingCount; ?>
</a>
</div>
<!-- Modal for displaying followers/following lists -->
<div id="followModal" style="display: none;">
<div class="modal-content">
<span class="close" onclick="closeModal()">×</span>
<h2 id="modalTitle"></h2>
<div id="followList"></div>
</div>
</div>
<script>
function showFollowers(userID) {
fetchFollowList(userID, 'followers');
}
function showFollowing(userID) {
fetchFollowList(userID, 'following');
}
function fetchFollowList(userID, type) {
fetch(`fetchFollowList.php?userID=${userID}&type=${type}`)
.then(response => response.text())
.then(data => {
document.getElementById('modalTitle').innerText = type === 'followers' ? 'Followers' : 'Following';
document.getElementById('followList').innerHTML = data;
document.getElementById('followModal').style.display = 'block';
});
}
function closeModal() {
document.getElementById('followModal').style.display = 'none';
}
</script>
<div class="profile-container">
<!-- Profile Details Display -->
<div class="profile-details">
<?php
if ($bio) echo "<p>" . htmlspecialchars($bio) . "</p>";
if ($igLink) echo "<p>Instagram: <a href='" . htmlspecialchars($igLink) . "'>" . htmlspecialchars($igLink) . "</a></p>";
if ($tiktokLink) echo "<p>TikTok: <a href='" . htmlspecialchars($tiktokLink) . "'>" . htmlspecialchars($tiktokLink) . "</a></p>";
if ($tumblrLink) echo "<p>Tumblr: <a href='" . htmlspecialchars($tumblrLink) . "'>" . htmlspecialchars($tumblrLink) . "</a></p>";
if ($twitterLink) echo "<p>Twitter: <a href='" . htmlspecialchars($twitterLink) . "'>" . htmlspecialchars($twitterLink) . "</a></p>";
if ($fbLink) echo "<p>Facebook: <a href='" . htmlspecialchars($fbLink) . "'>" . htmlspecialchars($fbLink) . "</a></p>";
if ($personalLink) echo "<p>Personal Website: <a href='" . htmlspecialchars($personalLink) . "'>" . htmlspecialchars($personalLink) . "</a></p>";
?>
</div>
</div>
<!-- Display User's Posts in Feed Style -->
<div class="user-posts">
<h2><?php echo htmlspecialchars($profileDisplayName); ?>'s Posts</h2>
<?php
// Query to fetch posts for the user in the same style as feed.php
$postQuery = "SELECT post.PostID, post.Caption, post.UserMedia, post.Timestamp, post.LikeCount
FROM post
WHERE post.UserID = ?
ORDER BY post.Timestamp DESC";
$postStmt = $db->prepare($postQuery);
$postStmt->bind_param("i", $profileUserID);
$postStmt->execute();
$postResult = $postStmt->get_result();
if ($postResult->num_rows > 0) {
echo "<div class='feed'>";
while ($postRow = $postResult->fetch_assoc()) {
echo "<div class='posts'>";
// Display profile picture and name
echo "<div class='post-left'>";
if (!empty($profilePicture)) {
echo "<img src='" . htmlspecialchars($profilePicture) . "' alt='Profile Picture' id='profilePic'>";
}
echo "<a href='userProfile.php?displayName=" . urlencode($profileDisplayName) . "'>" . htmlspecialchars($profileDisplayName) . "</a>";
echo "</div>";
// Display caption
echo "<div class='post-content'>";
echo "<p>" . htmlspecialchars($postRow['Caption']) . "</p>";
// Media reference link if it exists
if (!empty($postRow['UserMedia'])) {
echo "<img src='" . htmlspecialchars($postRow['UserMedia']) . "' alt='Post Image' style='width:100%; height:auto; margin-top:10px;'>";
}
echo "</div>";
// Timestamp
echo "<p class='timestamp'>Posted on: " . htmlspecialchars($postRow['Timestamp']) . "</p>";
echo "</div>"; // Close posts div
}
echo "</div>"; // Close feed div
} else {
echo "<p>No posts available.</p>";
}
$postStmt->close();
?>
</div>
<?php include 'elements/footer.php'; ?>
<script>
document.addEventListener("DOMContentLoaded", function() {
const followButton = document.getElementById("followButton");
if (followButton) {
followButton.addEventListener("click", function() {
const followingID = followButton.getAttribute("data-userid");
const action = followButton.getAttribute("data-action");
// Send AJAX request to follow/unfollow the user
fetch('followUser.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `followingID=${followingID}&action=${action}`
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
// Toggle button text and action attribute
followButton.innerText = action === 'follow' ? 'Unfollow' : 'Follow';
followButton.setAttribute("data-action", action === 'follow' ? 'unfollow' : 'follow');
} else {
alert(data.message);
}
})
.catch(error => {
console.error("Error:", error);
});
});
}
});
</script>
<script src="scripts.js"></script>
</body>
</html>