-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.php
197 lines (152 loc) · 5.32 KB
/
test.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
<?php
session_start();
include_once('./vendor/autoload.php');
\Dotenv\Dotenv::createImmutable(__DIR__)->load();
include('./includes/db_conn.php');
$potential_matches = array();
$pot_sql = "SELECT IF(TABLE2.userA_id = {$_SESSION['user_id']}, TABLE2.userB_id, TABLE2.userA_id) AS other_user_id FROM ( SELECT userA_id, userB_id FROM potential_matches WHERE (userA_id = {$_SESSION['user_id']} OR userB_id = {$_SESSION['user_id']}) ) AS TABLE2";
$q = mysqli_query($db_conn, $pot_sql);
if($q){
if(mysqli_num_rows($q)) {
while($row = mysqli_fetch_assoc($q)){
array_push($potential_matches, $row);
}
}
}
echo json_encode($potential_matches);
exit();
var_dump($_SESSION);
$interests = array();
$traits = array();
$matching_users = array();
$matching_user_ids = array();
$already_connected_user_ids = array();
// Get users interests
$interests_q = "SELECT interest_id FROM interests WHERE user_id = {$_SESSION['user_id']}";
$res = mysqli_query($db_conn, $interests_q);
if($res){
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_assoc($res)){
array_push($interests, $row['interest_id']);
}
}
}
// Get users traits
$interests_t = "SELECT trait_id FROM traits WHERE user_id = {$_SESSION['user_id']}";
$res = mysqli_query($db_conn, $interests_t);
if($res){
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_assoc($res)){
array_push($traits, $row['trait_id']);
}
}
}
echo "<h1>Interests</h1>";
var_dump($interests);
echo "<h1>Traits</h1>";
var_dump($traits);
$user_details = array();
// Get current users details
$sql = "SELECT age, gender, seeking FROM profiles WHERE user_id = {$_SESSION['user_id']}";
$r = mysqli_query($db_conn, $sql);
if($r){
if(mysqli_num_rows($r) > 0) {
$user_details = mysqli_fetch_assoc($r);
}
}
echo "<h4>User Details</h4>";
var_dump($user_details);
$age_range_low = $user_details['age'] - 20;
$age_range_high = $user_details['age'] + 20;
$sql = "SELECT user_id FROM profiles WHERE gender = '{$user_details['seeking']}' AND seeking = '{$user_details['gender']}' AND age BETWEEN {$age_range_low} AND {$age_range_high}";
echo($sql);
$r = mysqli_query($db_conn, $sql);
if($r){
if(mysqli_num_rows($r) > 0){
while($row = mysqli_fetch_assoc($r)){
array_push($matching_user_ids, $row['user_id']);
}
}
}
echo("<h2>Matching Users</h2>");
echo(json_encode($matching_user_ids));
// Get list of users who the current user is already matched with
$sql = "SELECT IF(TABLE2.userA_id = {$_SESSION['user_id']}, TABLE2.userB_id, TABLE2.userA_id) AS other_user_id, TABLE2.connection_id FROM ( SELECT connection_id, userA_id, userB_id FROM connections WHERE (userA_id = {$_SESSION['user_id']} OR userB_id = {$_SESSION['user_id']}) ) AS TABLE2";
$query = mysqli_query($db_conn, $sql);
if($query) {
if(mysqli_num_rows($query) > 0) {
// User has connections linked to their account
while($row = mysqli_fetch_assoc($query)) {
array_push($already_connected_user_ids, $row['other_user_id']);
}
}
}
// Stop user from sending match to themselves
array_push($already_connected_user_ids, $_SESSION['user_id']);
// Find users who have matching interests
$sql = "SELECT user_id FROM interests WHERE interest_id IN (" . implode(",", $interests) . ")";
$res = mysqli_query($db_conn, $sql);
if($res){
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_assoc($res)){
if(isset($matching_users[$row['user_id']])){
$matching_users[$row['user_id']] += 20;
} else {
$matching_users[$row['user_id']] = 20;
}
}
}
}
// Find users who have matching traits
$sql = "SELECT user_id FROM traits WHERE trait_id IN (" . implode(",", $traits) . ")";
$res = mysqli_query($db_conn, $sql);
if($res){
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_assoc($res)){
if(isset($matching_users[$row['user_id']])){
$matching_users[$row['user_id']] += 20;
} else {
$matching_users[$row['user_id']] = 20;
}
}
}
}
// Get other matching information
echo("<h2>Matching Users</h2>");
echo(json_encode($matching_users));
echo("<h2>Already Connected Users</h2>");
echo(json_encode($already_connected_user_ids));
$matching_user_ids = array();
// Sort users by score
arsort($matching_users);
// Remove users with low matching scores, not many interests/traits similiar
// Remove users who the user is already connected / pending connection with
foreach($matching_users as $index => $user){
if(in_array($index, $already_connected_user_ids) || $user < 45){
unset($matching_users[$index]);
} else {
array_push($matching_user_ids, $index);
}
}
echo "<h1>Sorted Users</h1>";
var_dump($matching_users);
// Create connections
$conn_sql = "INSERT INTO potential_matches (userA_id, userB_id, weight) VALUES";
foreach ($matching_users as $user_id => $weight) {
$conn_sql .= " ('{$_SESSION['user_id']}', '{$user_id}', {$weight}),";
}
$conn_sql = rtrim($conn_sql, ",");
$conn_q = mysqli_query($db_conn, $conn_sql);
if($conn_q){
if(mysqli_affected_rows($db_conn) > 0){
echo "Done";
}
}
?>
<ol>
<?php
foreach($matching_users as $index=>$value){
echo "<li><span>{$index} -> {$value}</span></li>";
}
?>
</ol>