-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
131 lines (94 loc) · 3.77 KB
/
search.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
<?php
include("includes/header.php");
if(isset($_GET['q'])) {
$query = $_GET['q'];
}
else {
$query = "";
}
if(isset($_GET['type'])) {
$type = $_GET['type'];
}
else {
$type = "name";
}
?>
<div class="main_column column" id="main_column">
<?php
if($query == "")
echo "You must enter something in the search box.";
else {
//If query contains an underscore, assume user is searching for usernames
if($type == "username")
$usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE username LIKE '$query%' AND user_closed='no' LIMIT 8");
//If there are two words, assume they are first and last names respectively
else {
$names = explode(" ", $query);
if(count($names) == 3)
$usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE (first_name LIKE '$names[0]%' AND last_name LIKE '$names[2]%') AND user_closed='no'");
//If query has one word only, search first names or last names
else if(count($names) == 2)
$usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE (first_name LIKE '$names[0]%' AND last_name LIKE '$names[1]%') AND user_closed='no'");
else
$usersReturnedQuery = mysqli_query($con, "SELECT * FROM users WHERE (first_name LIKE '$names[0]%' OR last_name LIKE '$names[0]%') AND user_closed='no'");
}
//Check if results were found
if(mysqli_num_rows($usersReturnedQuery) == 0)
echo "We can't find anyone with a " . $type . " like: " .$query;
else
echo mysqli_num_rows($usersReturnedQuery) . " results found: <br> <br>";
echo "<p id='grey'>Try searching for:</p>";
echo "<a href='search.php?q=" . $query ."&type=name'>Names</a>, <a href='search.php?q=" . $query ."&type=username'>Usernames</a><br><br><hr id='search_hr'>";
while($row = mysqli_fetch_array($usersReturnedQuery)) {
$user_obj = new User($con, $user['username']);
$button = "";
$mutual_friends = "";
if($user['username'] != $row['username']) {
//Generate button depending on friendship status
if($user_obj->isFriend($row['username']))
$button = "<input type='submit' name='" . $row['username'] . "' class='danger' value='Remove Friend'>";
else if($user_obj->didReceiveRequest($row['username']))
$button = "<input type='submit' name='" . $row['username'] . "' class='warning' value='Respond to request'>";
else if($user_obj->didSendRequest($row['username']))
$button = "<input type='submit' class='default' value='Request Sent'>";
else
$button = "<input type='submit' name='" . $row['username'] . "' class='success' value='Add Friend'>";
$mutual_friends = $user_obj->getMutualFriends($row['username']) . " friends in common";
//Button forms
if(isset($_POST[$row['username']])) {
if($user_obj->isFriend($row['username'])) {
$user_obj->removeFriend($row['username']);
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
}
else if($user_obj->didReceiveRequest($row['username'])) {
header("Location: requests.php");
}
else if($user_obj->didSendRequest($row['username'])) {
}
else {
$user_obj->sendRequest($row['username']);
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
}
}
}
echo "<div class='search_result'>
<div class='searchPageFriendButtons'>
<form action='' method='POST'>
" . $button . "
<br>
</form>
</div>
<div class='result_profile_pic'>
<a href='" . $row['username'] ."'><img src='". $row['profile_pic'] ."' style='height: 100px;'></a>
</div>
<a href='" . $row['username'] ."'> " . $row['first_name'] . " " . $row['last_name'] . "
<p id='grey'> " . $row['username'] ."</p>
</a>
<br>
" . $mutual_friends ."<br>
</div>
<hr id='search_hr'>";
} //End while
}
?>
</div>