Add standing offers
[contractdashboard.git] / search_autosuggest.php
blob:a/search_autosuggest.php -> blob:b/search_autosuggest.php
<?php <?php
   
include_once ("./lib/common.inc.php"); include_once ("./lib/common.inc.php");
$input = strtolower($_REQUEST['input']); $input = strtolower($_REQUEST['input']);
$len = strlen($input); $len = strlen($input);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0; $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$aResults = array(); $aResults = array();
$count = 0; $count = 0;
if ($len) { if ($len) {
$query = 'SELECT mode("supplierName"), MAX("supplierABN"), count(*) as count $query = 'SELECT mode("supplierName"), MAX("supplierABN"), count(*) as count
FROM contractnotice FROM contractnotice
WHERE "supplierName" ILIKE :supplierName WHERE "supplierName" ILIKE :supplierName
GROUP BY "supplierName" GROUP BY "supplierName"
ORDER BY count(*) DESC ORDER BY count(*) DESC
LIMIT 4; LIMIT 4;
'; ';
$query = $conn->prepare($query); $query = $conn->prepare($query);
$supplierName = $input . '%'; $supplierName = $input . '%';
$query->bindParam(":supplierName", $supplierName); $query->bindParam(":supplierName", $supplierName);
$query->execute(); $query->execute();
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
foreach ($query->fetchAll() as $row) { foreach ($query->fetchAll() as $row) {
$count++; $count++;
$aResults[] = array( $aResults[] = array(
"id" => "supplier-" . $row[1] . '-' . $row[0], "id" => "supplier-" . $row[1] . '-' . $row[0],
"value" => htmlspecialchars($row[0]), "value" => htmlspecialchars($row[0]),
"info" => htmlspecialchars("Supplier - " . $row['count'] . " records") "info" => htmlspecialchars("Supplier - " . $row['count'] . " records")
); );
} }
$query = 'SELECT MAX("agencyName"), count(*) as count $query = 'SELECT MAX("agencyName"), count(*) as count
FROM contractnotice FROM contractnotice
WHERE "agencyName" ILIKE :agencyName WHERE "agencyName" ILIKE :agencyName
GROUP BY "agencyName" GROUP BY "agencyName"
ORDER BY count DESC ORDER BY count DESC
LIMIT 4;'; LIMIT 4;';
$query = $conn->prepare($query); $query = $conn->prepare($query);
$agencyName = $input . '%'; $agencyName = $input . '%';
$query->bindParam(":agencyName", $agencyName); $query->bindParam(":agencyName", $agencyName);
$query->execute(); $query->execute();
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
foreach ($query->fetchAll() as $row) { foreach ($query->fetchAll() as $row) {
$count++; $count++;
$aResults[] = array( $aResults[] = array(
"id" => "agency-" . $row[0], "id" => "agency-" . $row[0],
"value" => htmlspecialchars($row[0]), "value" => htmlspecialchars($row[0]),
"info" => htmlspecialchars("Government Agency - " . $row['count'] . " records") "info" => htmlspecialchars("Government Agency - " . $row['count'] . " records")
); );
} }
$query = "SELECT \"CNID\", description, value FROM contractnotice $query = "SELECT \"CNID\", description, value FROM contractnotice
WHERE to_tsvector('english', description) @@ plainto_tsquery('english', :input)"; WHERE to_tsvector('english', description) @@ plainto_tsquery('english', :input)";
$query = $conn->prepare($query); $query = $conn->prepare($query);
$agencyName = $input . '%';  
$query->bindParam(":input", $input); $query->bindParam(":input", $input);
$query->execute(); $query->execute();
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
foreach ($query->fetchAll() as $row) { foreach ($query->fetchAll() as $row) {
$count++; $count++;
$aResults[] = array( $aResults[] = array(
"id" => "cnid-" . $row[0], "id" => "cnid-" . $row[0],
"value" => htmlspecialchars($row['description']), "value" => htmlspecialchars($row['description']),
"info" => htmlspecialchars("Contract Notice - Value ".$row['value']) "info" => htmlspecialchars("Contract Notice - Value ".$row['value'])
); );
} }
} }
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache"); // HTTP/1.0 header("Pragma: no-cache"); // HTTP/1.0
if (isset($_REQUEST['json'])) { if (isset($_REQUEST['json'])) {
header("Content-Type: application/json"); header("Content-Type: application/json");
echo "{\"results\": ["; echo "{\"results\": [";
$arr = array(); $arr = array();
for ($i = 0; $i < count($aResults); $i++) { for ($i = 0; $i < count($aResults); $i++) {
$arr[] = "{\"id\": \"" . $aResults[$i]['id'] . "\", \"value\": \"" . $aResults[$i]['value'] . "\", \"info\": \"\"}"; $arr[] = "{\"id\": \"" . $aResults[$i]['id'] . "\", \"value\": \"" . $aResults[$i]['value'] . "\", \"info\": \"\"}";
} }
echo implode(", ", $arr); echo implode(", ", $arr);
echo "]}"; echo "]}";
} else { } else {
header("Content-Type: text/xml"); header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>"; echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
for ($i = 0; $i < count($aResults); $i++) { for ($i = 0; $i < count($aResults); $i++) {
echo "<rs id=\"" . $aResults[$i]['id'] . "\" info=\"" . $aResults[$i]['info'] . "\">" . $aResults[$i]['value'] . "</rs>"; echo "<rs id=\"" . $aResults[$i]['id'] . "\" info=\"" . $aResults[$i]['info'] . "\">" . $aResults[$i]['value'] . "</rs>";
} }
echo "</results>"; echo "</results>";
} }
?> ?>