Skip to content

Commit d8d60a8

Browse files
First commit
1 parent 0e57a81 commit d8d60a8

File tree

9 files changed

+1182
-0
lines changed

9 files changed

+1182
-0
lines changed

img/m-worldmap.jpg

74.8 KB
Loading

img/search.png

794 Bytes
Loading

img/worldmap.jpg

97 KB
Loading

index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="black" />
9+
<meta name="description" content="Search for the city you are interested in and get basic information about it such as: status of the city, population, country in which it is located and country abbreviation, coordinates of the city.">
10+
<link rel="icon" href="./img/search.png">
11+
<link rel="apple-touch-icon" href="./img/search.png">
12+
<link rel="stylesheet" href="./style.css">
13+
<title>Search city info</title>
14+
</head>
15+
16+
<body>
17+
<main>
18+
<section class="search-block">
19+
<h1>Find a city and get information about it</h1>
20+
<p>(Capitals, cities, towns)</p>
21+
<form id="search-form">
22+
<label>
23+
<input id="search" type="text" spellcheck="false" autocomplete="off" placeholder="City name">
24+
<button id="reset" type="reset" title="Click me to clear the input field">&#215;</button>
25+
</label>
26+
27+
28+
<output>
29+
<ul id="hint"></ul>
30+
</output>
31+
</form>
32+
</section>
33+
34+
<section class="result-block">
35+
<ul id="result"></ul>
36+
</section>
37+
</main>
38+
<div class="mask"></div>
39+
<script src="./script.js"></script>
40+
</body>
41+
42+
</html>

php/core.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require_once './function.php';
4+
5+
$request = trim(file_get_contents("php://input"));
6+
$request = json_decode($request, true);
7+
8+
$action = $request['action'];
9+
10+
switch($action) {
11+
case 'inputSearch':
12+
inputSearch();
13+
break;
14+
case 'loadCityInfo':
15+
loadCityInfo();
16+
break;
17+
18+
}
19+

php/createKeywords.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/*
3+
This function needed to create keywords from a string, which is target for search.
4+
Having keywords column in DB table makes search more efficient, because:
5+
-Saves time for processing the target string during the search.
6+
-Creates the possibility of adding new keywords by which the target string can be found without changing it.
7+
-Creates the possibility of excluding exception words (or service words).
8+
9+
!!! Before you use the code make sure that you have created 'keywords' column in target table.
10+
11+
In this example I have created keywords for the table 'cities'.
12+
*/
13+
$conn = mysqli_connect("host", "login", "password", "database");
14+
mysqli_query($conn, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
15+
mysqli_query($conn, "SET CHARACTER SET 'utf8'");
16+
17+
// This process can take for a while if you have a lot of items in the table. You can increase the time if necessary.
18+
ini_set('max_execution_time', 1000);
19+
20+
// words that do not carry a semantic load for search (use optionally)
21+
$servWords = array("and", "or", "of", "a", "the", "some", "any");
22+
$errors = [];
23+
24+
$sql = "SELECT `city`, `id` FROM `cities`";
25+
$res = mysqli_query($conn, $sql);
26+
27+
28+
while ($row = mysqli_fetch_assoc($res)) {
29+
$textArr = [];
30+
$keysArr = [];
31+
32+
$id = $row['id'];
33+
$name = preg_replace("/[^\w\s]/iu", "", $row['city']);
34+
$textArr = preg_split("/[\W]/iu", mb_strtolower($name), null, PREG_SPLIT_NO_EMPTY);
35+
36+
foreach ($textArr as $aKey => $a) {
37+
if (!in_array($a, $servWords)) {
38+
$keysArr[] = $a;
39+
}
40+
// OR (if you don't want to use exceptions)
41+
// $keysArr[] = $a;
42+
}
43+
44+
$keys = implode("+", $keysArr);
45+
$keys = '+' . $keys;
46+
47+
$sql2 = "UPDATE `cities` SET `keywords`='$keys' WHERE `id`='$id'";
48+
$res2 = mysqli_query($conn, $sql2);
49+
50+
if ($res2 == false) {
51+
$errors[] = 'error with city ' . $row['city'] . '-' . $row['id'];
52+
}
53+
}
54+
55+
if (empty($errors)) {
56+
echo '1';
57+
} else {
58+
echo json_encode($errors);
59+
}
60+
61+
mysqli_close($conn);

0 commit comments

Comments
 (0)