Created
October 15, 2014 03:34
-
-
Save IlyaPavlik/a734137c3fb17efece72 to your computer and use it in GitHub Desktop.
"Умный" поиск
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment