Skip to content

Commit

Permalink
Create search.php
Browse files Browse the repository at this point in the history
  • Loading branch information
lfernandogalvis committed May 20, 2020
1 parent 681abe8 commit d66942a
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions API/contact/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");

$requestMethod = $_SERVER["REQUEST_METHOD"];
include('../class/Contacts.php');
$contact = new Contacts();

// get posted data
$data = json_decode(file_get_contents("php://input"));

if ($conn->connect_error){
returnWithError( $conn->connect_error );
}

else{

$sql = "Search for contacts where Name like '%" . $inData["search"] . "%' and UserID = " . $inData["userId"];
$result = $conn->query($sql);

if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
if( $searchCount > 0 ){
$searchResults .= ",";
}

$searchCount++;
$searchResults .= '"' . $row["Name"] . '"';
}
}

else{
returnWithError( "No Conctacts Found." );
}

$conn->close();
}

returnWithInfo( $searchResults );

function getRequestInfo(){
return json_decode(file_get_contents('php://input'), true);
}

function sendResultInfoAsJson( $obj ){
header('Content-type: application/json');
echo $obj;
}

function returnWithError( $err ){
$retValue = '{"id":0,"firstName":"","lastName":"","error":"' . $err . '"}';
sendResultInfoAsJson( $retValue );
}

function returnWithInfo( $searchResults ){
$retValue = '{"results":[' . $searchResults . '],"error":""}';
sendResultInfoAsJson( $retValue );
}
}
?>

0 comments on commit d66942a

Please sign in to comment.