Below is my PHP code which find the result from mysql table what is typed by user search to search field. It is also finds if user has typed something randomly and find the matching row fetch from the database and show to user. But there is one problem it will fetch the record from the databse as they are sequentially store into the database. but i want is it will show the result which matches the most of the keywords or sentence which is type by user.
for example user searching for "search and pagination with php" and all the records are fetch from the database
results:
1. PHP RSS Feed Read and List
2.DropDown with Search using jQuery
3.PHP CRUD with Search and Pagination
4.PHP MySQL Date Range Search with jQuery DatePicker"
This is how my code is working. But i want is that result 3rd should be came 1st positon beacuse it matches the greater number of keywords matching and others should also came after that because it have some matching keywords. I have also attached the screenshot of same result screenshot.
If we are searching something in google we are search just randomly. everyone searching type is different but topic is same so google will return the exact result even if type in another way or spelling is typed wrong. How can I do that or what are changes I have to make in code. and If I want to add auto suggestions like google then How can I do that.
I want a search field which works like google or StackOverflow for finding the result which has similar or matching results in PHP.
<?php
$conn = mysqli_connect("localhost", "root", "", "blog_samples");
$with_any_one_of = "";
$with_the_exact_of = "";
$without = "";
$starts_with = "";
$search_in = "";
$advance_search_submit = "";
$queryCondition = "";
if(!empty($_POST["search"])) {
$advance_search_submit = $_POST["advance_search_submit"];
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("with_any_one_of","with_the_exact_of","without","starts_with");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "with_any_one_of":
$with_any_one_of = $v;
$wordsAry = explode(" ", $v);
$wordsCount = count($wordsAry);
for($i=0;$i<$wordsCount;$i++) {
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $wordsAry[$i] . "%'";
} else {
$queryCondition .= "title LIKE '" . $wordsAry[$i] . "%' OR description LIKE '" . $wordsAry[$i] . "%'";
}
if($i!=$wordsCount-1) {
$queryCondition .= " OR ";
}
}
break;
}
}
}
}
$orderby = " ORDER BY id desc";
$sql = "SELECT * FROM links " . $queryCondition;
$result = mysqli_query($conn,$sql);
?>
<html>
<head>
<title>Advanced Search using PHP</title>
<script>
function showHideAdvanceSearch() {
if(document.getElementById("advanced-search-box").style.display=="none") {
document.getElementById("advanced-search-box").style.display = "block";
document.getElementById("advance_search_submit").value= "1";
} else {
document.getElementById("advanced-search-box").style.display = "none";
document.getElementById("with_the_exact_of").value= "";
document.getElementById("without").value= "";
document.getElementById("starts_with").value= "";
document.getElementById("search_in").value= "";
document.getElementById("advance_search_submit").value= "";
}
}
</script>
</head>
<body>
<h2>Advanced Search using PHP</h2>
<div>
<form name="frmSearch" method="post" action="index.php">
<input type="hidden" id="advance_search_submit" name="advance_search_submit" value="<?php echo $advance_search_submit; ?>">
<div class="search-box">
<label class="search-label">With Any One of the Words:</label>
<div>
<input type="text" name="search[with_any_one_of]" class="demoInputBox" value="<?php echo $with_any_one_of; ?>" />
</div>
<div>
<input type="submit" name="go" class="btnSearch" value="Search">
</div>
</div>
</form>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<div>
<div><strong><?php echo $row["title"]; ?></strong></div>
<div class="result-description"><?php echo $row["description"]; ?></div>
</div>
<?php } ?>
</div>
</body>
</html>
trial 2