-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_friends.php
190 lines (187 loc) · 9.39 KB
/
select_friends.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
<?php
include('session.php');
if (!isset($_SESSION['login_user'])){
echo '<script type="text/javascript">
window.location = "index.php"
</script>';
}
$selfIDQuery = mysqli_query($conn, "SELECT accountID FROM account WHERE email_address = '$user_check'");
$row = mysqli_fetch_array($selfIDQuery);
if (isset($_SESSION['login_user'])){
$selfID = $row['accountID'];
} else {
$selfID = "error";
}
$selfFriendsQuery = mysqli_query($conn, "SELECT friend2ID FROM Friendship WHERE friend1ID = ('$selfID') ");
$selfFriendsQuery2 = mysqli_query($conn, "SELECT friend1ID FROM Friendship WHERE friend2ID = ('$selfID') ");
$XcircleQuery = mysqli_query($conn, "SELECT circleID FROM CircleMembership WHERE accountID = ('$selfID')");
$selfFriendsQuery_2 = mysqli_query($conn, "SELECT friend2ID FROM Friendship WHERE friend1ID = ('$selfID') ");
$selfFriendsQuery2_2= mysqli_query($conn, "SELECT friend1ID FROM Friendship WHERE friend2ID = ('$selfID') ");
$XcircleQuery_2 = mysqli_query($conn, "SELECT circleID FROM CircleMembership WHERE accountID = ('$selfID')");
?>
<html>
<head>
<?php require_once('head.php');?>
<title>Create Friend Cicle</title>
</head>
<body>
<script src="js/jqBootstrapValidation.js"></script>
<?php require_once('common_navbar.html');?>
<div class="container">
<script>
$("#selectedFriends_header").addClass("active");
</script>
<div class="row">
<script>
$(function () { $("input,select,textarea").not("[type=submit]").jqBootstrapValidation(); } );
</script>
<h1>Form circle of friends</h1>
<div class="col-md-6">
<h2>Create Circle:</h2>
<form method = "post" role = "form">
<div class="form-group">
<label class="control-label">Name of the circle: </label>
<input type="text" name="circleName" class="box" placeholder="Circle Name" required/><br/>
</div>
<div class="form-group">
<?php echo "You have ".(mysqli_num_rows($selfFriendsQuery)+mysqli_num_rows($selfFriendsQuery2))." friends<br/>"; ?>
<p>Select friends to form a circle of friends<p>
<p class="help-block">select at least one friend</p>
</div>
<div class="checkbox">
<?php
while($friendRow = mysqli_fetch_array($selfFriendsQuery)){
$friendID = $friendRow["friend2ID"];
$friendNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$friendID') ");
$nameRow = mysqli_fetch_array($friendNameQuery);
echo "<input type=\"checkbox\" name=\"selectedFriends[]\" value=\"".$friendID."\" data-validation-minchecked-minchecked=\"2\" data-validation-minchecked-message=\"Choose at least one\" >".$nameRow['name']."<br>";
}
while($friendRow = mysqli_fetch_array($selfFriendsQuery2)){
$friendID = $friendRow["friend1ID"];
$friendNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$friendID') ");
$nameRow = mysqli_fetch_array($friendNameQuery);
echo "<input type=\"checkbox\" name=\"selectedFriends[]\" value=\"".$friendID."\" data-validation-minchecked-minchecked=\"2\" data-validation-minchecked-message=\"Choose at least one\" >".$nameRow['name']."<br>";
}?>
</div>
<br/>
<input name="create-submit" type="submit" class="btn btn-default" value="Create">
<br/>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['create-submit'])){
$filteredName = mysqli_real_escape_string($conn,$_POST['circleName']);
if(!empty($_POST['selectedFriends'])) {
$FriendCircleQuery = mysqli_query($conn,"INSERT INTO FriendCircle (accountID,nameOfCircle) VALUES ($selfID,'$filteredName')");
$circleID = mysqli_insert_id($conn);
$insertSelfIntoCircleQuery = mysqli_query($conn,"INSERT INTO CircleMembership (circleID,accountID) VALUES ($circleID,$selfID)");
foreach($_POST['selectedFriends'] as $eachFriend) {
mysqli_query($conn,"INSERT INTO CircleMembership (circleID,accountID) VALUES ($circleID,'$eachFriend')");
}
echo "<div class=\"alert alert-success\" role=\"alert\">Circle created! Go to Chat room and chat!</div>";
}
}
?>
</div>
<div class="col-md-6">
<h2>Current friend cirle</h2>
<?php
while($XcircleRow = mysqli_fetch_array($XcircleQuery)){
$XcircleID = $XcircleRow["circleID"];
$XcircleNameQuery = mysqli_query($conn,"SELECT nameOfCircle FROM FriendCircle WHERE circleID = $XcircleID ORDER BY nameOfCircle");
$XcircleNameRow = mysqli_fetch_array($XcircleNameQuery);
$XnameOfCircle = $XcircleNameRow['nameOfCircle'];
echo "<h5>".$XnameOfCircle."<h5/>";
$XcircleFriendIDQuery = mysqli_query($conn, "SELECT accountID FROM CircleMembership WHERE circleID = ('$XcircleID')");
echo "<p class=\"help-block\">Circle member: ";
$selfNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$selfID') ");
$selfNameRow = mysqli_fetch_array($selfNameQuery);
$selfName=$selfNameRow['name'];
echo $selfName." ";
while ($XcircleFriendIDRow = mysqli_fetch_array($XcircleFriendIDQuery)) {
if ($XcircleFriendIDRow['accountID']!=$selfID) {
$XfriendID=$XcircleFriendIDRow['accountID'];
$XfriendNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$XfriendID') ");
$XfriendNameRow = mysqli_fetch_array($XfriendNameQuery);
$XfriendName=$XfriendNameRow['name'];
echo $XfriendName." ";
}
}
echo "</p>";
}
?>
</div>
</div>
<div class="row">
<h1>Add friends into current circle</h1>
<script>
$(function () { $("input,select,textarea").not("[type=submit]").jqBootstrapValidation(); } );
</script>
<div class="col-md-6">
<form method = "post" role = "form">
<p class="help-block">Select the circle that you want to add friends in</p>
<?php
while($XcircleRow = mysqli_fetch_array($XcircleQuery_2)){
$XcircleID = $XcircleRow["circleID"];
$XcircleNameQuery = mysqli_query($conn,"SELECT nameOfCircle FROM FriendCircle WHERE circleID = $XcircleID ORDER BY nameOfCircle");
$XcircleNameRow = mysqli_fetch_array($XcircleNameQuery);
$XnameOfCircle = $XcircleNameRow['nameOfCircle'];
echo "<input type=\"radio\" name=\"selectedCircle\" value=\"".$XcircleID."\"> ".$XnameOfCircle."<br/>";
$XcircleFriendIDQuery = mysqli_query($conn, "SELECT accountID FROM CircleMembership WHERE circleID = ('$XcircleID')");
echo "<p class=\"help-block\">Circle member: ";
$selfNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$selfID') ");
$selfNameRow = mysqli_fetch_array($selfNameQuery);
$selfName=$selfNameRow['name'];
echo $selfName." ";
while ($XcircleFriendIDRow = mysqli_fetch_array($XcircleFriendIDQuery)) {
if ($XcircleFriendIDRow['accountID']!=$selfID) {
$XfriendID=$XcircleFriendIDRow['accountID'];
$XfriendNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$XfriendID') ");
$XfriendNameRow = mysqli_fetch_array($XfriendNameQuery);
$XfriendName=$XfriendNameRow['name'];
echo $XfriendName." ";
}
}
echo "</p>";
}
?>
</div>
<div class="col-md-6">
<div class="form-group">
<p class="help-block">Select friends to add into current circle<p>
</div>
<div class="checkbox">
<?php
while($friendRow = mysqli_fetch_array($selfFriendsQuery_2)){
$friendID = $friendRow["friend2ID"];
$friendNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$friendID') ");
$nameRow = mysqli_fetch_array($friendNameQuery);
echo "<input type=\"checkbox\" name=\"selectedFriends2[]\" value=\"".$friendID."\">".$nameRow['name']."<br>";
}
while($friendRow = mysqli_fetch_array($selfFriendsQuery2_2)){
$friendID = $friendRow["friend1ID"];
$friendNameQuery = mysqli_query($conn, "SELECT name FROM Account WHERE accountID = ('$friendID') ");
$nameRow = mysqli_fetch_array($friendNameQuery);
echo "<input type=\"checkbox\" name=\"selectedFriends2[]\" value=\"".$friendID."\">".$nameRow['name']."<br>";
}?>
</div>
<br/>
<input name="add-submit" type="submit" class="btn btn-default" value="Add">
<br/>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['add-submit'])){
if(!empty($_POST['selectedFriends2']) && !empty($_POST['selectedCircle'])) {
$circleID = $_POST['selectedCircle'];
foreach($_POST['selectedFriends2'] as $eachFriend) {
mysqli_query($conn,"INSERT INTO CircleMembership (circleID,accountID) VALUES ($circleID,'$eachFriend')");
}
echo "<br/><div class=\"alert alert-success\" role=\"alert\">friend added!</div>";
}
}
?>
</div>
</div>
</div>
<?php require_once('common_footer.html');?>
</body>
</html>