I have a websites that stores a lot of pdf articles. The fields for these articles e.g title, author etc are stored in a MySQL database. I would like to construct a search that returns the nearest matches for the field 'title' and have the search return a result.
For example I may have articles with the following titles:
- 'Deregulated proliferation and differentiation in brain tumors.'
- 'Tumor-induced myeloid dysfunction and its implications for cancer immunotherapy.'
- 'Epigenetic alterations in inflammatory bowel disease and cancer.'
So if i entered into the search: 'cancer and epigenetics', articles with titles similar to (3), would show up in the result.
At the moment, when I generate an sql search query, only exact matches will return a result. Please could someone advise me on how to move forward with this problem? Some broad strokes and pointers would be very much welcome.
My php code:
// HANDLES SEARCH INPUT
if(isset($_POST['searchstring'])){
$searchterm = $_POST['searchstring'];
if($mysqli->connect_errno){
/*echo 'error connecting to the database';*/
echo 'error db connection';
exit();
}
$searchterm = $mysqli -> real_escape_string($searchterm);
$sql = "SELECT id FROM pdf_library WHERE title='$searchterm'";
$result = $mysqli -> query($sql);
if($result){
$num_rows = $result -> num_rows;
if($num_rows <1){
echo 'no_result';
}else{
echo 'you_have_results';
}
}else{
echo 'searh failed';
exit();
}
}
My javascript code:
var search_btn = document.getElementById('searchbtn');
search_btn.addEventListener('click', function(){
var string = document.getElementById('search_input').value;
console.log('search string: ' + string);
//AJAX REQUEST
var formdata = new FormData();
formdata.append('searchstring', string);
// xmlhttpRequest object
var xmlhttp = new XMLHttpRequest;
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
if(xmlhttp.responseText == 'no_result'){
console.log('no results found');
}else if(xmlhttp.responseText == 'you_have_results'){
console.log('results found');
}else{
console.log(xmlhttp.responseText);
}
}
}
xmlhttp.open('POST', 'search_aux.php');
xmlhttp.send(formdata);
});