|
| 1 | +# Database search algorithm (PHP/SQL) |
| 2 | +Smart and fast search algorithm based on PHP. In this case, a database of world cities search using SQL queries is presented. There are some features of the algorithm: |
| 3 | ++ Retrieve the 10 most relevant results or similar results if there are no exact matches. |
| 4 | ++ The result will be found even if a small error is made in the request or the request is inaccurate. |
| 5 | ++ The presence of an array of exception words or service words that will be found last if there are no other results. |
| 6 | ++ High search speed. In the country of the hosting provider, the average request speed is 60 - 100 milliseconds. |
| 7 | + (In this case, there are 43 thousand cities in the database) |
| 8 | + |
| 9 | +The code also comes with a function to create keywords from a string, which is target for search. Having keywords column in DB table makes search more efficient, because: |
| 10 | ++ Saves time for processing the target string during the search. |
| 11 | ++ Creates the possibility of adding new keywords by which the target string can be found without changing it. |
| 12 | ++ Creates the possibility of excluding exception words (or service words). |
| 13 | + |
| 14 | +Search interface implemented using JavaScript, HTML, CSS. This demo is made in a minimalistic but functional design with a convenient mobile version. |
| 15 | +The code also provides a JavaScript function for switching between the found results using the cursor. |
| 16 | + |
| 17 | +## Working demo |
| 18 | +https://www.search-city-info.online/ |
| 19 | + |
| 20 | +## How to use |
| 21 | +1. **Using the search** |
| 22 | +In this example, the JavaScript file sends a fetch request with the search text and the "action" variable to the core.php file, which calls the necessary function |
| 23 | +in the function.php file according to the value in "action". This allows you to put different functions in the function.php file and call them independently. |
| 24 | +In this case, 2 functions are used: search as you type and search for information about the city when you click on one of the cities found. |
| 25 | +The search function always returns an object in the JavaScript file with the keys: results, text, keys. The "results" key corresponds to the results array, |
| 26 | +"text" - the escaped text of the query is returned, keys - an array with keys that are similar to the query (they are used to highlight the match in the found text). |
| 27 | +This way you can use any kind of php requests and have any JavaScript file structure. You need to do: |
| 28 | ++ In function.php connect to the database by entering the hostname, login, password and database name. |
| 29 | ++ Specify the correct path to the core.php file, send request: "serach text ", action: "name of the function". |
| 30 | ++ Make sure you have keywords column in target table.(possible to search without keywords, but performance will be worse). |
| 31 | + |
| 32 | +2. **Creating keywords** |
| 33 | +In createKeywords.php you can find a function which slect a string from target column and divide it in single words which are separated by "+". |
| 34 | +There is an array with exception or service words, which can be used to exclude unwanted words from keywords column. How to use the function(an example): |
| 35 | ++ Open the createKeywords.php file and connect to the database by entering the hostname, login, password and database name. |
| 36 | ++ Create a new column in target table with String type (make sure the length of the column value should be slightly more than target column). |
| 37 | ++ Connect createKeywords.php to your project or insert the code from createKeywords.php to your file with .php extention and reload the page. |
| 38 | ++ Note that the process may take some time depending on the number of items in the table. |
| 39 | ++ Delete createKeywords.php code from your code or disconnect createKeywords.php |
| 40 | + |
| 41 | +### License |
| 42 | +MIT © Oleksandr Chernokolov - [BlackPrick](https://github.com/BlackPrick). |
0 commit comments