Skip to content

Instantly share code, notes, and snippets.

@gekh
Forked from IlyaPavlik/index.html
Last active August 29, 2015 14:07
Show Gist options
  • Save gekh/74efe11644cfabbf19a8 to your computer and use it in GitHub Desktop.
Save gekh/74efe11644cfabbf19a8 to your computer and use it in GitHub Desktop.
Стажерское задание выполненное Илье Павликом
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>"Умный" поиск</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="smartSearch.js"></script>
</head>
<body>
<div class="content">
<div class="search">
<form action="search.php" method="POST">
<input type="text" name="search" id="search" autocomplete="off">
</form>
<div id="resSearch">Начните вводить запрос</div>
</div>
</div>
</body>
</html>
<?php
header("Content-type: text/html; charset=utf8");
$search = $_POST['search'];
$search = addslashes($search);
$search = htmlspecialchars($search);
$search = stripslashes($search);
if($search == ''){
exit("Начните вводить запрос");
}
$db = mysql_connect("localhost","root","");
mysql_select_db("test",$db);
mysql_query("SET NAMES 'utf8'");
$searchRex = buildRegex(mb_strtolower($search, 'UTF-8'));
$query = mysql_query("SELECT firstname, lastname FROM user WHERE MATCH (firstname,lastname) AGAINST ('$search') || id = '$search' || LOWER(firstname) REGEXP '$searchRex' || LOWER(lastname) REGEXP '$searchRex'",$db);
if(mysql_num_rows($query) > 0){
$sql = mysql_fetch_array($query);
do{
echo "<div>".$sql['firstname']." ".$sql['lastname']."</div>";
}while($sql = mysql_fetch_array($query));
}else{
echo "Нет результатов";
}
function buildRegex($search){
$size = strlen($search);
for($i=0; $i<$size; $i++){
$tmp_str = substr($search,$i,1)."|";
$tmp_search .= $tmp_str;
}
$search = $tmp_search;
$search = "[".$search."]";
$tmp_search = "";
for($i=0; $i<$size; $i++){
$tmp_search .= $search;
}
return $tmp_search;
}
$(function(){
$("#search").keyup(function(){
var search = $("#search").val();
$.ajax({
type: "POST",
url: "search.php",
data: {"search": search},
cache: false,
success: function(response){
$("#resSearch").html(response);
}
});
return false;
});
});
body {
background-color: silver;
}
.content {
margin: 0 auto;
width: 1000px;
}
.search input {
display: block;
margin: 0 auto;
}
.search input[type="text"] {
font-weight:bold;
font-size:16px;
font-family:"Tahoma", sans-serif;
/*text-transform:uppercase; */
border-top: 1px solid #aaa;
border-bottom: 1px solid #eee;
color: #555;
display: block;
height: 48px;
margin: 0 auto 17px;
text-align: center;
width: 500px;
border-radius: 5px;
}
#resSearch {
margin: 0 auto 17px;
width: 500px;
}
@gekh
Copy link
Author

gekh commented Oct 17, 2014

[13/10/14 17:40:42 ] Герман Хохлов: Сделать таблицу в БД, из трех столбцов: id, firstname, lastname
на сайте сделать форму из одного инпута, когда вводишь какой-либо символ, отправляется ajax-запрос на сервер, который выбирает из базы все подходящие записи. Причем ищет как по id, так и по имени и фамилии, все, что совпадает пусть выводится под формой.
[13/10/14 17:40:52 ] Герман Хохлов: Если вводится имя и фамилия, то тоже должен искать
[13/10/14 17:42:13 ] Герман Хохлов: Если все сделаешь, и нечем будет заняться, то делай поиск с ошибками, например пишу «Иавн», а находит «Иван».
[13/10/14 17:42:44 ] Герман Хохлов: Либо как в гугле: «Возможно вы имели в виду Иван»

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment