I have already a php page to call my bibliographical database.
Here is the code of the main file:
<?php
/* [SEARCH FOR USERS] */
if (isset($_POST['search'])) {
require "bibliografie-search.2.php";
}
/* [DISPLAY HTML] */ ?>
<!-- [SEARCH FORM] -->
<form method="post" arction="bibliografie-search.2.php">
<input type="text" name="search" required/>
<input type="submit" value="cerca"/>
</form>
<!-- [SEARCH RESULTS] -->
<?php
function hash_link_format($key){
return "<a href=\"hashtag-biblio.php?tag=$key\">$key</a>";
}
function buildLinks($str, $jdel=',' ,$formatFooName='hash_link_format', $del=',' ){
return implode($jdel.' ', array_map($formatFooName, explode($del, $str)));
}
if (isset($_POST['search'])) {
if (count($results) > 0) {
foreach ($results as $r) {
$key = explode(',', $r['keywords']);
//printf("<div><p><b>%s</b></p><h2>%s</h2><blockquote><p>%s</p></blockquote><p>%s</p></div>", $r['autore'], $r['titolo'], $r['contenuti'], buildLinks($r['keywords']));
printf("<div><p><b>%s</b>, %s</p>\n<h2>%s</h2>\n<p>%s, %s %s</p>\n<blockquote><p>%s</p></blockquote>\n<p>%s</p></div>", $r['autore'], $r['autore_nome'], $r['titolo'], $r['edizione'], $r['luogo'], $r['data'], $r['contenuti'], /*$r['fonte'], $r['fonte_spec'],*/ buildLinks($r['keywords']));
}
} else {
echo "No results found";
}
}
?>
and the code of the of the other file:
<?php
// (1) DATABASE CONFIG
// ! CHANGE THESE TO YOUR OWN !
define('DB_HOST', 'localhost');
define('DB_NAME', 'bibliografia');
define('DB_CHARSET', 'utf8');
define('DB_USER', 'myuser');
define('DB_PASSWORD', 'mypsw');
// (2) CONNECT TO DATABASE
try {
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";charset=" . DB_CHARSET . ";dbname=" . DB_NAME,
DB_USER, DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false ]
);
} catch (Exception $ex) {
die($ex->getMessage());
}
// (3) SEARCH
$stmt = $pdo->prepare("SELECT * FROM `bibliografie` WHERE `autore` LIKE ? OR `titolo` LIKE ? OR `keywords` LIKE ?");
$stmt->execute(["%" . $_POST['search'] . "%", "%" . $_POST['search'] . "%", "%" . $_POST['search'] . "%"]);
$results = $stmt->fetchAll();
if (isset($_POST['ajax'])) { echo json_encode($results); }
?>
I’d like to add some further option, adding other columns (field), i.g. search 1) a title, 2) an author, 3) a keyword and so on not merging them (as it is now), but choosing where search to.
What code should I use?
Thank you