<?php

global $db;
$query = $_REQUEST["query"];

$results = array();
$sql = "SELECT * FROM users join user_detail on users.user_id = user_detail.user_id WHERE users.user_id <> '".$_SESSION['user_id']."'  AND users.role <> 0";
if( !empty($query) ){
	$sql.=" AND (users.user_id LIKE '%".$query."%' ";
	$sql.=" OR users.username LIKE '%".$query."%' ";
	$sql.=" OR user_detail.first_name LIKE '%".$query."%') ";
}
$data = $db->query($sql);
foreach ($data as $key => $value) {
	if (checkFriend($_SESSION['user_id'],$value["user_id"]) == 0){
		$results[] = array(
			"value" => $value["first_name"]. " - (" .$value["username"]. ")",
			"desc" => $value["user_id"],
			"img" => showImage($value["user_id"]) ,
			"tokens" => array($query, $query . rand(1, 10))
		);
	}
}
echo json_encode($results);