-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
158 lines (145 loc) · 6.53 KB
/
Copy pathprofile.php
File metadata and controls
158 lines (145 loc) · 6.53 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
<?php
session_start();
require 'partials/_navbar.php'; ?>
<?php
// Start session if not already started
// Check if the user is logged in
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
$username = $_SESSION['username'];
if (isset($_SESSION['uid'])) {
$userid = $_SESSION['uid']; // Ensure $userid is set
} else {
// If UID is missing, redirect to index
header("location: index.php");
exit();
}
} else {
// If not logged in, redirect to index
header("location: index.php");
exit();
}
// Fetch profile picture and username
$getProfilePicSql = mysqli_query($conn, "SELECT user_profile_pic, username FROM `users` WHERE user_id = '$userid'");
if ($getProfilePicSql && mysqli_num_rows($getProfilePicSql) > 0) {
$get_profile_pic = mysqli_fetch_assoc($getProfilePicSql);
$profile_pic_name = $get_profile_pic['user_profile_pic'];
$updatedUsername = $get_profile_pic['username'];
} else {
// If no profile picture found, set default values
$profile_pic_name = 'default_pic.png'; // Default picture path
$updatedUsername = $username; // Fallback to session username
}
// Fetch threads by user
$sql = "SELECT * FROM `threads` WHERE thread_user_id=$userid";
$result = mysqli_query($conn, $sql);
$threads = []; // To store all threads
while ($row = mysqli_fetch_assoc($result)) {
$threads[] = $row; // Store each thread in the array
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/nav.css">
<link rel="stylesheet" href="css/profile.css">
<link rel="stylesheet" href="css/home.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<title>Profile - <?php echo htmlspecialchars($_SESSION['username']); ?></title>
</head>
<body>
<?php
// Dialog for updating profile
echo '<dialog data-modal class="dialog-container">
<h1 id="post-dialog-heading">Update Your Profile</h1>
<form class="new-post-form" action="handleprofile.php" method="post" enctype="multipart/form-data">
<label for="profile-username">Edit Username:</label>
<input type="text" name="profile-username" id="profile-username" value="'.htmlspecialchars($username).'">
<label for="profile-pic-update">Select Profile Picture</label>
<input type="file" name="profile-pic-update" id="profile-pic-update" class="post-image-upload" accept=".jpg, .jpeg, .png">
<input type="hidden" value="'.htmlspecialchars($userid).'" name="uid" id="uid">
<button type="submit" name="submit" id="submit">Save</button>
<button formmethod="dialog" id="cancel-post-btn" onclick="location.href=\'profile.php\'">Cancel</button>
</form>
</dialog>';
?>
<section class="profile-section">
<div class="profile-container">
<div class="profile-pic-main-container">
<img src="img/<?php echo htmlspecialchars($profile_pic_name); ?>" alt="Profile Picture">
</div>
<div class="user-profile-info-container">
<h3><?php echo htmlspecialchars($updatedUsername); ?></h3>
<button data-open-modal>Edit profile</button>
</div>
</div>
<section class="posts">
<?php
// Function to format time
function formatDateTime($datetime) {
date_default_timezone_set('Asia/Kolkata');
$dateTimeTimestamp = strtotime($datetime);
$nowTimestamp = time();
$timeDifference = $nowTimestamp - $dateTimeTimestamp;
if ($timeDifference < 60) {
return 'Just now';
} elseif ($timeDifference < 3600) {
$minutes = floor($timeDifference / 60);
return $minutes == 1 ? '1 minute ago' : $minutes . ' minutes ago';
} elseif ($timeDifference < 86400) {
$hours = floor($timeDifference / 3600);
return $hours == 1 ? '1 hour ago' : $hours . ' hours ago';
} elseif ($timeDifference < 2592000) {
$days = floor($timeDifference / 86400);
return $days == 1 ? '1 day ago' : $days . ' days ago';
} elseif ($timeDifference < 31536000) {
$months = floor($timeDifference / 2592000);
return $months == 1 ? '1 month ago' : $months . ' months ago';
} else {
$years = floor($timeDifference / 31536000);
return $years == 1 ? '1 year ago' : $years . ' years ago';
}
}
// Display threads
foreach ($threads as $thread) {
$threadDateTime = formatDateTime($thread['timestamp']);
$threadId = htmlspecialchars($thread['thread_id']);
$threadTitle = htmlspecialchars($thread['thread_title']);
$threadDesc = htmlspecialchars($thread['thread_desc']);
$threadImg = htmlspecialchars($thread['thread_img']);
// Display each post
echo '<div class="post-container">
<div class="post-info-container">
<div class="post-profile-picture">
<img src="img/'.htmlspecialchars($profile_pic_name).'" alt="">
</div>
<div class="post-info">
<h4 class="post-username">'.htmlspecialchars($username).'</h4>
<p>Posted '.$threadDateTime.'</p>
</div>
</div>
<div class="post-image-container">
<img src="img/'.htmlspecialchars($threadImg).'" alt="Post Image">
</div>
<div class="post-title-container">
<h3>'.$threadTitle.'</h3>
<p>'.$threadDesc.'</p>
</div>
</div>';
}
?>
</section>
</section>
<script>
const openButton = document.querySelectorAll("[data-open-modal]");
const closeButton = document.querySelector("[data-close-modal]");
const modal = document.querySelector("[data-modal]");
for (let i = 0; i < openButton.length; i++) {
openButton[i].addEventListener("click", function() {
modal.showModal();
});
}
</script>
</body>
</html>