1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ChatApp; |
4
|
|
|
require_once (dirname(__DIR__) . '/vendor/autoload.php'); |
5
|
|
|
use ChatApp\Time; |
6
|
|
|
use ChatApp\Session; |
7
|
|
|
use Dotenv\Dotenv; |
8
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
9
|
|
|
$dotenv->load(); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Search for the user |
13
|
|
|
*/ |
14
|
|
|
class Search |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $connect; |
18
|
|
|
protected $array; |
19
|
|
|
protected $obTime; |
20
|
|
|
|
21
|
|
View Code Duplication |
public function __construct($sessionId) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
session_id($sessionId); |
24
|
|
|
@session_start(); |
|
|
|
|
25
|
|
|
$this->connect = mysqli_connect( |
26
|
|
|
getenv('DB_HOST'), |
27
|
|
|
getenv('DB_USER'), |
28
|
|
|
getenv('DB_PASSWORD'), |
29
|
|
|
getenv('DB_NAME') |
30
|
|
|
); |
31
|
|
|
session_write_close(); |
32
|
|
|
$this->obTime = new Time(); |
33
|
|
|
$this->array = array(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function searchItem($suggestion) |
37
|
|
|
{ |
38
|
|
|
$suggestion = $suggestion->value; |
39
|
|
|
$flag = 0; |
40
|
|
|
$userId = Session::get('start'); |
41
|
|
|
if($userId != null && isset($suggestion)) |
42
|
|
|
{ |
43
|
|
|
$suggestion = trim($suggestion); |
44
|
|
|
if($suggestion != "") |
45
|
|
|
{ |
46
|
|
|
$query = "SELECT * FROM login where login_id != '$userId' and name like '$suggestion%' ORDER BY name ASC"; |
47
|
|
|
if($result = $this->connect->query($query)) |
48
|
|
|
{ |
49
|
|
|
if($result->num_rows > 0) |
50
|
|
|
{ |
51
|
|
|
while($row = $result->fetch_assoc()) |
52
|
|
|
{ |
53
|
|
|
$check_id = $row["login_id"]; |
54
|
|
|
$query = "SELECT * from total_message where (user1 = '$check_id' and user2 = '$userId') or (user2 = '$check_id' and user1 = '$userId')"; |
55
|
|
|
if($result1 = $this->connect->query($query)) |
56
|
|
|
{ |
57
|
|
|
if($result1->num_rows > 0) |
58
|
|
|
{ |
59
|
|
|
$fetch = $result1->fetch_assoc(); |
60
|
|
|
$fetch['time'] = $this->obTime->timeConversion($fetch['time']); |
61
|
|
|
|
62
|
|
|
$this->array = array_merge($this->array, [['time' => $fetch['time'], 'username' => $row['username'], 'name' => $row['name']]]); |
63
|
|
|
$flag = 1; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
if($flag != 0) |
70
|
|
|
{ |
71
|
|
|
$this->array = array_merge([], ["Search" => $this->array]); |
72
|
|
|
return json_encode($this->array); |
73
|
|
|
} |
74
|
|
|
else |
75
|
|
|
return json_encode(["Search" => "Not Found"]); |
76
|
|
|
} |
77
|
|
|
else |
78
|
|
|
{ |
79
|
|
|
return json_encode(["Search" => "Not Found"]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
return json_encode(["Search" => "Not Found"]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.