-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
98 lines (86 loc) · 4.89 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
<?php
//Connect to db
include_once "connect.php";
$arr; // Start with no results found
// Check if searchTerm and switchButton variables are set
if (isset($_POST["searchTerm"]) && isset($_POST["switchButton"])) {
$searchTerm = trim($_POST["searchTerm"]);
$switchButton = trim($_POST["switchButton"]);
$searchTerm = $mysqli->real_escape_string("%" . $searchTerm . "%"); // Get search term
//Check if the switchButton is on Grids
if ($switchButton == 'showGrids'){
$query = "SELECT gridid, first_name, last_name, profile_picture, account_type FROM grids WHERE CONCAT(first_name, ' ', IFNULL(last_name,'')) LIKE ? ORDER BY first_name";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param("s", $searchTerm); // Bind parameters
$stmt->execute(); // Execute statement
$stmt->bind_result($gridid, $first_name, $last_name, $profile_picture, $account_type); // Bind result variables
/* fetch values */
$count = 0;
while ($stmt->fetch()) {
$count += 1; // Count grids found
// Create array with grid's data
$arr[$count] = array('status' => 1, 'gridid' => $gridid, 'first_name' => $first_name, 'last_name' => $last_name, 'profile_picture' => $profile_picture, 'account_type' => $account_type);
}
$mysqli->close(); // Close connection
for($i=1; $i<=$count; $i++){
//Get number of members in each grid that was found
/*-----------------------------------------------------------------------------------------------
$memberQuery = "SELECT COUNT(*) AS memberCount FROM grid_members WHERE gridid=? ";
if ($memberStmt = $mysqli->prepare($memberQuery)) {
$memberStmt->bind_param("s", $arr[$i]['gridid']); // Bind parameters
$memberStmt->execute(); // Execute statement
$memberStmt->bind_result($countOfMembers); // Bind result variables
// fetch values
while ($memberStmt->fetch()) {
$memberCount = $countOfMembers;
}
}
$mysqli->close(); // Close connection
-----------------------------------------------------------------------------------------------*/
$memberCount = $arr[$i]['gridid'];
//Insert this memberCount into array
$memberCountArr[$i] = array('memberCount' => $memberCount);
$arr[$i] = array_merge($arr[$i],$memberCountArr[$i]);
}
}
//switchButton is on Events
}elseif ($switchButton == 'showEvents') {
$query = "SELECT eventid, event_title, event_picture FROM events WHERE event_title LIKE ? ORDER BY event_title";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param("s", $searchTerm); // Bind parameters
$stmt->execute(); // Execute statement
$stmt->bind_result($eventid, $event_title, $event_picture); // Bind result variables
/* fetch values */
$count = 0;
while ($stmt->fetch()) {
$count += 1; // Count events found
// Create array with grid's data
$arr[$count] = array('status' => 1, 'eventid' => $eventid, 'event_title' => $event_title, 'event_picture' => $event_picture);
}
$mysqli->close(); // Close connection
for($i=1; $i<=$count; $i++){
//Get number of attendees in this event
/*----------------------------------------------------------------------------------------------
$attendeeQuery = "SELECT COUNT(*) FROM event_members WHERE eventid = ? ";
if ($attendeeStmt = $mysqli->prepare($attendeeQuery)) {
$attendeeStmt->bind_param("s", $arr[$i]['eventid']); // Bind parameters
$attendeeStmt->execute(); // Execute statement
$attendeeStmt->bind_result($eventid); // Bind result variables
// fetch values
$attendeeStmt->bind_result($countOfAttendees);
while ($attendeeStmt->fetch()) {
$attendeeCount = $countOfAttendees;
}
}
----------------------------------------------------------------------------------------------*/
$attendeeCount = $arr[$i]['eventid'];
//Insert this attendeeCount into array
$attendeeCountArr[$i] = array('attendeeCount' => $attendeeCount );
$arr[$i] = array_merge($arr[$i],$attendeeCountArr[$i]);
}
}
}
}
if ($count > 0)
echo json_encode($arr);
?>