92 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2023-01-24 19:00:39 +01:00
<?php
class search {
private $PDO;
public function __construct() {
global $PDO;
$this->PDO = $PDO;
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function query () {
$txt = $_POST['txt']??'';
// search by NAME, LABEL, TXT
$query = 'SELECT id, block, name, label FROM l_ess_item WHERE id=:id ';
try {
$stmt = $this->PDO->prepare($query);
$stmt->execute(array($id));
// let's make it easier to format by doing it here instead of in SQL
if ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
// return json array so the client forms it
return array (
'id' => $r['id'],
'name' => $r['name'],
'label' => $r['label'],
'block' => $r['block']);
}
}
catch (exception $ex) {
common::except ('Err: ' .$ex .' in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
}
}
public function txtSearch($suggestions=false) {
$back = array(); // this we return
$txt = $_POST['txt']??'';
if (strlen ($txt) > 0) {
// search by NAME, LABEL, TXT
$query = 'SELECT DISTINCT '
."id, block, name, label, txt, 100 as factor FROM l_ess_item WHERE name LIKE (?) UNION SELECT "
."id, block, name, label, txt, 80 as factor FROM l_ess_item where label LIKE (?) UNION SELECT "
."id, block, name, label, txt, 60 as factor FROM l_ess_item where txt LIKE (?) UNION SELECT "
."id, block, name, label, txt, 40 as factor FROM l_ess_item where name LIKE (?) UNION SELECT "
."id, block, name, label, txt, 20 as factor FROM l_ess_item where label LIKE (?) UNION SELECT "
."id, block, name, label, txt, 10 as factor FROM l_ess_item where txt LIKE (?) "
."ORDER BY factor desc " .($suggestions == true?' limit 10':'');
try {
$stmt = $this->PDO->prepare($query);
$txta= $txt .'%';
$txtb= '%' .$txt .'%';
$stmt->execute(array($txta, $txta, $txta, $txtb, $txtb, $txtb));
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
// return json array so the client forms it as they wish
$back[] = array (
'id' => $r['id'],
'name' => $r['name'],
'label' => $r['label'],
'block' => $r['block'],
'txt' => $r['txt']);
}
return $back;
}
catch (exception $ex) {
common::except ('Err: ' .$ex .' in ' .__DIR__ .'/' .__FILE__ .':' .__LINE__);
}
}
}
public function basicSearch() {
return '<div><a onclick="#">Prvi predlog</a></div>';
}
}