0

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

  • 1
    Consider [MySQL Full Text Search](https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html) – Gabriel Alejandro López López Sep 26 '20 at 18:52
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 26 '20 at 19:04

1 Answers1

1

You can compare the results of the search the user inputs and the query results before displaying them. Compare how many words are similar for each result from the array and print the one that has the most first.

$advance_search_submit
$wordsAry[$i]

You could easily use both variables to compare how many words in wordsAry are similar to advance_search_submit and store them in a displayResults array in order of which have more words in common.

Or

Could do the same thing but compare it to the title of the results.

$results
$wordsAry[$i]

Comparing these two variables in a similar way to above. Compare words to result (contains) and save them in order in another array to be displayed.

sosumi
  • 57
  • 1
  • 13
  • Thank you @IvoyCoding now I got the idea of how I will display the most specific result at 1st position and others afterward of that. But this code is not working as google works for the same topic searched by the different types of peoples in their own way but got the same result. – Nikhil Vishwakarma Sep 26 '20 at 19:58