-
Notifications
You must be signed in to change notification settings - Fork 7
/
search_task.php
75 lines (61 loc) · 2.21 KB
/
search_task.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
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-Type: application/json');
/*
* Following code will get single task details
* A task is identified by task id (taskId)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
$postdata = json_decode(file_get_contents("php://input"), true);
$userId = $postdata['userId'];
$q = $postdata['q'];
// check for post data
if (isset($userId) & isset($q)) {
// get a task from task table
$result = mysqli_query($db->connect(),"SELECT * FROM tasks WHERE userId = '$userId' AND name LIKE '%$q%' ");
if (!empty($result)) {
// check for empty result
if (mysqli_num_rows($result) > 0) {
$response["tasks"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp tasks array
$tasks = array();
$task["taskId"] = $row["taskId"];
$task["name"] = $row["name"];
$task["description"] = $row["description"];
$task["dateCreated"] = $row["dateCreated"];
$task["dateUpdated"] = $row["dateUpdated"];
// push single task into final response array
array_push($response["tasks"], $task);
}
// echoing JSON response
echo json_encode($response);
} else {
// no task found
$response["success"] = 0;
$response["message"] = "No task found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no task found
$response["success"] = 0;
$response["message"] = "No task found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>