chiro-canto/public/upload/getspecieshint.php
2021-04-15 12:29:53 +02:00

55 lines
1.1 KiB
PHP
Executable File

<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/database/credentials.php";
// Connect the database
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
$req = $db->prepare('SELECT taxon_species FROM `taxa`');
$req->execute();
$result = $req->fetchAll();
$species = array();
foreach ($result as $row) {
$species[] = $row['taxon_species'];
}
$q = $_REQUEST['q'];
$hint = array();
// print_r($species);
if ($q !== "") {
$q = strtolower($q);
$len = strlen($q);
foreach($species as $sp) {
if (stristr($q, substr($sp, 0, $len))) {
$hint[] = $sp;
}
}
}
// Output "no suggestion" if no hint was found or output correct values
?>
<select name="species-hint" id="species-hint">
<?php
foreach ($hint as $sp) {
?>
<option value="<?=$sp?>"><?=$sp?></option>
<?php
}
?>
</select>
<?