Skip to content

Commit

Permalink
Added SearchContacts.php
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRiz15 authored Jan 24, 2024
1 parent bb00102 commit 2ae1c08
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions html/LAMPAPI/SearchContacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

$inData = getRequestInfo();
$searchResults = "";
$searchCount = 0;



$conn = new mysqli("localhost", "TheBeast", "WeLoveCOP4331", "COP4331");
if ($conn->connect_error)
{
returnWithError( $conn->connect_error );
}
else
{
$stmt = $conn->prepare("SELECT * FROM Contacts WHERE (FirstName LIKE ? OR LastName LIKE ? ) AND CreatedByUserID=?");
$searchName = "%" . $inData["search"] . "%";
$stmt->bind_param("ssi", $searchName, $searchName, $inData["createdByUserId"]);
$stmt->execute();

$result = $stmt->get_result();

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

$searchResults .= '{"FirstName" : "' . $row["FirstName"]. '", "LastName" : "' . $row["LastName"]. '", "EmailAddress" : "' . $row["EmailAddress"]. '", "PhoneNumber" : "' . $row["PhoneNumber"]. '", "UserID" : "' . $row["UserID"].'", "ID" : "' . $row["ID"]. '"}';
}

if( $searchCount == 0 )
{
returnWithError( "No Records Found" );
}
else
{
returnWithInfo( $searchResults );
}

$stmt->close();
$conn->close();
}

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 2ae1c08

Please sign in to comment.