-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriendadd.php
More file actions
192 lines (158 loc) · 6.08 KB
/
friendadd.php
File metadata and controls
192 lines (158 loc) · 6.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description" content="Web application development - Assignment 1" />
<meta name="keywords" content="Html,CSS,PHP,SQL" />
<meta name="author" content="Ashaen Manuel" />
<link rel="stylesheet" href="style.css" type="text/css">
<title>My Friend System</title>
</head>
<body>
<?php
// Start session
session_start();
// Retrieve session variables from previous page
$name = $_SESSION["profile_name"];
$email = $_SESSION["my_email"];
// Get database login details
require_once("settings.php");
// Create a connection to the database
$conn = new mysqli($host, $user, $pswd, $dbnm);
// Get total number of friends count for pagination
$getTotalFriendsSql = "
SELECT COUNT(f.friend_id) AS total_friends
FROM friends f
WHERE f.profile_name != '$name'
AND f.friend_id NOT IN (
SELECT mf.friend_id2
FROM myfriends mf
JOIN friends f1 ON mf.friend_id1 = f1.friend_id
WHERE f1.profile_name = '$name'
)";
// Run query and get the total number of friends
$totalResult = $conn->query($getTotalFriendsSql);
$totalFriends = $totalResult->fetch_assoc()['total_friends'];
// Set number of friends to display per page
$friendsPerPage = 10;
// Calculate and round up the total number of pages
$totalPages = ceil($totalFriends / $friendsPerPage);
// Get the current page number from the URL (1 if not set)
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
// Calculate the offset for the SQL query
$offset = ($page - 1) * $friendsPerPage;
// SQL query to fetch the friends for the current page along with the count of mutual friends
$getAllFriendsExceptMeSql = "
SELECT f.profile_name,
(
SELECT COUNT(*)
FROM myfriends mf1
JOIN myfriends mf2 ON mf1.friend_id2 = mf2.friend_id2
WHERE mf1.friend_id1 = (
SELECT friend_id FROM friends WHERE profile_name = '$name'
)
AND mf2.friend_id1 = f.friend_id
) AS mutual_friends_count
FROM friends f
WHERE f.profile_name != '$name'
AND f.friend_id NOT IN (
SELECT mf.friend_id2
FROM myfriends mf
JOIN friends f1 ON mf.friend_id1 = f1.friend_id
WHERE f1.profile_name = '$name'
)
ORDER BY f.profile_name
LIMIT $friendsPerPage OFFSET $offset
";
$result = $conn->query($getAllFriendsExceptMeSql);
// Function to get the friend count for the logged-in user
function getFriendsCount($emailInput, $conn)
{
$getFriendsCountSql = "SELECT num_of_friends FROM friends WHERE friend_email = '$emailInput'";
$result = $conn->query($getFriendsCountSql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return $row['num_of_friends'];
}
}
$_SESSION["friends_count"] = getFriendsCount($email, $conn);
$friendsCount = $_SESSION["friends_count"];
// If user clicks the Add Friend button
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['addfriend'])) {
$friendToAdd = $_POST['addfriend'];
// Get ID of the current user and friend
$getCurrentUserIdSql = "SELECT friend_id FROM friends WHERE profile_name = '$name'";
$getFriendIdSQL = "SELECT friend_id FROM friends WHERE profile_name = '$friendToAdd'";
$result1 = $conn->query($getCurrentUserIdSql);
$result2 = $conn->query($getFriendIdSQL);
if ($result1->num_rows > 0 && $result2->num_rows > 0) {
$row1 = $result1->fetch_assoc();
$row2 = $result2->fetch_assoc();
$id1 = $row1['friend_id'];
$id2 = $row2['friend_id'];
// Insert the user ID and the friends ID into myfriends table
$addFriendSql = "
INSERT INTO myfriends (friend_id1, friend_id2) VALUES ('$id1', '$id2')
";
// Increment and update the count of friends for the user in the friends table by 1
$updateUserSql = "
UPDATE friends SET num_of_friends = num_of_friends + 1
WHERE profile_name = '$name'
";
// Update both tables with the new friend ID linking and the count
if ($conn->query($addFriendSql) === TRUE) {
$conn->query($updateUserSql);
$_SESSION["friends_count"] = getFriendsCount($email, $conn);
header("Location: friendadd.php?page=$page");
exit();
}
}
}
?>
<main id="friendadd-main">
<h1>My Friend System</h1>
<h2><?php echo "$name"; ?>'s Add Friend Page</h2>
<h2>Total number of friends is <?php echo "$friendsCount"; ?></h2>
<form action="friendadd.php" method="post">
<table id="friendadd-table">
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row["profile_name"]) . "</td>";
echo "<td>Mutual Friends: " . htmlspecialchars($row["mutual_friends_count"]) . "</td>";
echo "<td><button type='submit' name='addfriend' value='" . htmlspecialchars($row["profile_name"]) . "' class='submit-buttons'>Add as friend</button></td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='3'>No friends available to add</td></tr>";
}
?>
</table>
</form>
<div id="pagination">
<?php if ($page > 1): ?>
<a href="friendadd.php?page=<?php echo $page - 1; ?>">Previous</a>
<?php endif; ?>
<?php if ($page < $totalPages): ?>
<a href="friendadd.php?page=<?php echo $page + 1; ?>">Next</a>
<?php endif; ?>
</div>
<hr>
<a href="friendadd.php?action=friendlist">Friend Lists</a>
<a href="friendadd.php?action=logout">Log Out</a>
</main>
<?php
// If user clicks the Friend Lists button, redirect to friendlist.php
if (isset($_GET['action']) && $_GET['action'] == 'friendlist') {
header("Location: friendlist.php");
exit;
}
// If user clicks the logout button, redirect to logout.php
if (isset($_GET['action']) && $_GET['action'] == 'logout') {
header("Location: logout.php");
exit;
}
?>
</body>
</html>