我已经设置了一个AJAX实时搜索,我想知道何时从数据库中提取信息是否可以将其放入不同的结果div中?
因为我想单独设计它们。我甚至希望一次出现一个,但我想首先解决这个问题。
我现在设置它的方式可以工作,但是element.style正在改变我的愿景:(
当我搜索时再说一遍,当它返回结果时,可以进入不同的结果div?
php
<?php
/************************************************
The Search PHP File
************************************************/
/************************************************
MySQL Connect
************************************************/
// Credentials
$dbhost = "localhost";
$dbname = "zackreid_test2";
$dbuser = "zackreid_test";
$dbtable = "test2";
$dbpass = "test2";
// Connection
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");
// Check Connection
if ($tutorial_db->connect_errno) {
printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}
/************************************************
Search Functionality
************************************************/
// Define Output HTML Formating
$html = '';
$html .= '<li class="result">';
$html .= '<a target="_blank" href="urlString">';
$html .= '<h3>nameString</h3>';
$html .= '<h4>functionString</h4>';
$html .= '</a>';
$html .= '</li>';
// Get Search
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_REQUEST['query']);
$search_string = $tutorial_db->real_escape_string($search_string);
// Check Length More Than One Character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
// Build Query
$query = 'SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%"';
// Do Search
$result = $tutorial_db->query($query);
// while($results = $result->fetch_array()) {
if ($result) {
while($results = $result->fetch_array()) {
$result_array[] = $results;
}
}
// Check If We Have Results
if (isset($result_array)) {
foreach ($result_array as $result) {
// Format Output Strings And Hightlight Matches
$display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['function']);
$display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['name']);
$display_url = 'http://www.zackreid.com/'.urlencode($result['function']).'.html';
// Insert Name
$output = str_replace('nameString', $display_name, $html);
// Insert Function
$output = str_replace('functionString', $display_function, $output);
// Insert URL
$output = str_replace('urlString', $display_url, $output);
// Output
echo($output);
}
}else{
// Format No Results Output
$output = str_replace('urlString', 'javascript:void(0);', $html);
$output = str_replace('nameString', '<b>No Results Found.</b>', $output);
$output = str_replace('functionString', 'Sorry :(', $output);
// Output
echo($output);
}
}
?>
HTML
<!DOCTYPE HTML>
<html>
<head>
<!-- Meta -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Periodic Table Search</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- Load CSS -->
<link href="style/style.css" rel="stylesheet" type="text/css" />
<!-- Load Fonts -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:regular,bold" type="text/css" />
<!-- Load jQuery library -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- Load custom js -->
<script type="text/javascript" src="scripts/custom.js"></script>
</head>
<body>
<div id="main">
<!-- Main Title -->
<h1 class="title">Periodic Table Search</h1>
<h5 class="title">A simple why to find the element you are looking for</h5>
<!-- Main Input -->
<div class="container-4">
<input type="text" id="search" autocomplete="off">
</div>
<!-- Show Results -->
<h4 id="results-text">Showing results for: <b id="search-string">Array</b></h4>
<ul id="results"></ul>
</div>
</body>
</html>
CSS
/******************************************************************
This is the CSS Reset
******************************************************************/
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
font-weight: normal;
-webkit-font-smoothing: antialiased;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
display: block;
}
body {
display: block;
font-family: 'Raleway', sans-serif;
background: rgba(92,126,158,1);
background: -moz-linear-gradient(45deg, rgba(92,126,158,1) 0%, rgba(53,91,128,1) 100%);
background: -webkit-gradient(left bottom, right top, color-stop(0%, rgba(92,126,158,1)), color-stop(100%, rgba(53,91,128,1)));
background: -webkit-linear-gradient(45deg, rgba(92,126,158,1) 0%, rgba(53,91,128,1) 100%);
background: -o-linear-gradient(45deg, rgba(92,126,158,1) 0%, rgba(53,91,128,1) 100%);
background: -ms-linear-gradient(45deg, rgba(92,126,158,1) 0%, rgba(53,91,128,1) 100%);
background: linear-gradient(45deg, rgb(91, 122, 152) 0%, rgb(58, 86, 113) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#5c7e9e', endColorstr='#355b80', GradientType=1 );
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
a {
outline: none;
text-decoration: none;
}
/******************************************************************
Text Selection Styles
******************************************************************/
/* Safari, Chrome, iPhones, iPads */
::selection {
background:#4096ee;
color:#fff;
}
::-moz-selection {
background:#4096ee;
color:#fff;
}
::-webkit-selection {
background:#4096ee;
color:#fff;
}
/******************************************************************
General CSS
******************************************************************/
p {
font-family: Verdana, Arial, sans-serif;
line-height: 1.6em;
color: #616161;
font-size: 10px;
}
h1 {
font-family: 'PT Sans', Verdana, Arial, sans-serif;
font-weight: bold;
line-height: 1.6em;
color: white;
text-decoration: none;
font-size: 20px;
}
h2 {
font-family: 'PT Sans', Verdana, Arial, sans-serif;
line-height: 1.6em;
color: white;
text-decoration: none;
font-size: 16px
}
h3 {
font-family: 'PT Sans', Verdana, Arial, sans-serif;
line-height: 1.6em;
color: white;
text-decoration: none;
font-size: 14px;
}
h4 {
font-family: 'PT Sans', Verdana, Arial, sans-serif;
line-height: 1.6em;
color: white;
text-decoration: none;
font-size: 12px;
}
h5 {
font-family: 'PT Sans', Verdana, Arial, sans-serif;
line-height: 1.6em;
color: white;
text-decoration: none;
font-size: 10px;
}
h6 {
font-family: 'PT Sans', Verdana, Arial, sans-serif;
line-height: 1.6em;
color: white;
text-decoration: none;
font-size: 8px;
}
/******************************************************************
Main CSS
******************************************************************/
div#main {
height: 100%;
width: 360px;
margin: 270px auto 20px auto;
}
.title {
line-height: 1.2em;
position: relative;
margin-left: 40px;
}
/* make keyframes that tell the start state and the end state of our object */
@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
.container-4 {
opacity:0; /* make things invisible upon start */
-webkit-animation:fadeIn 1s ease-in 0s 1 normal forwards; /* call our keyframe named fadeIn, use animattion ease-in and repeat it only 1 time */
-moz-animation:fadeIn 1s ease-in 0s 1 normal forwards;
animation:fadeIn 1s ease-in 0s 1 normal forwards;
}
.container-4 {
-webkit-animation-delay: 0.7s ;
-moz-animation-delay: 0.7s ;
animation-delay: 0.7s;
}
input#search {
width: 350px;
height: 25px;
padding: 5px;
margin-top: 15px;
margin-bottom: 15px;
box-shadow: 0px 0px 39px #284561;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
outline: none;
border: 1px solid #ababab;
font-size: 20px;
line-height: 25px;
color: #ababab;
}
input#search:hover, input#search:focus {
color: #3b3b3b;
border: 1px solid #36a2d2;
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgba(255, 255, 255, 1);
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgba(255, 255, 255, 1);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgba(255, 255, 255, 1);
}
h4#results-text {
display: none;
}
ul#results {
display: none;
justify-content: center;
background-color: #77D2B3;
width: 360px;
height: 120px;
margin-top: 4px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
-webkit-box-shadow: rgba(0, 0, 0, .15) 0 1px 3px;
-moz-box-shadow: rgba(0,0,0,.15) 0 1px 3px;
box-shadow: rgba(0, 0, 0, .15) 0 1px 3px;
}
ul#results:after {
display: flex !important;
}
ul#results li {
width: 100px;
display: flex;
justify-content: center;
align-items: center;
margin: 10px 10px 10px 10px;
padding: 8px 8px 8px 8px;
cursor: pointer;
transition: background-color .3s ease-in-out;
-moz-transition: background-color .3s ease-in-out;
-webkit-transition: background-color .3s ease-in-out;
}
ul#results li:hover {
background-color: #F7F7F7;
}
ul#results li:first-child {
border-top: none;
}
ul#results li h3, ul#results li h4 {
transition: color .3s ease-in-out;
-moz-transition: color .3s ease-in-out;
-webkit-transition: color .3s ease-in-out;
color: #FFF;
line-height: 1.2em;
}
ul#results li:hover h3, ul#results li:hover h4 {
color: #3b3b3b;
font-weight: bold;
}
修改
忘记提及这是我正在运行的测试版本,但这是现场直播;
http://dev.zackreid.com/test/download/Code/index.html
如果您搜索空气,它将返回三个结果。
EDIT2
忘记添加JavaScript;
/* JS File */
// Start Ready
$(document).ready(function() {
// Icon Click Focus
$('div.icon').click(function(){
$('input#search').focus();
});
// Live Search
// On Search Submit and Get Results
function search() {
var query_value = $('input#search').val();
$('b#search-string').text(query_value);
if(query_value !== ''){
$.ajax({
type: "POST",
url: "search.php",
data: { query: query_value },
cache: false,
success: function(html){
$("ul#results").html(html);
}
});
}return false;
}
$("input#search").live("keyup", function(e) {
// Set Timeout
clearTimeout($.data(this, 'timer'));
// Set Search String
var search_string = $(this).val();
// Do Search
if (search_string == '') {
$("ul#results").fadeOut();
$('h4#results-text').fadeOut();
}else{
$("ul#results").fadeIn();
$('h4#results-text').fadeIn();
$(this).data('timer', setTimeout(search, 100));
};
});
});
答案 0 :(得分:0)
我给你一个“想法/示例”如何在我阅读你的评论之后让你的代码正常工作:
PHP文件:
...
if (isset($result_array)) {
foreach ($result_array as $result) {
$isResult = true; // We set a variable for checking if we have a result or not
$arrayFetch[] = [
// Passing your result
'output_1' => $result['foo_1'],
'output_2' => $result['bar_1']
];
}
} else {
$isResult = false;
$arrayFetch[] = [
'output_3' => $result['foo_2'],
'output_4' => $result['bar_2']
];
}
// We encoding to json
echo json_encode([
'isResult' => $isResult,
'result' => $arrayFetch
]);
...
Javascript(jQuery)文件:
// Create a function to decode your result (I do it only for a cleaner code)
function getResult(data) {
// Initialize your variable result
var a = '',
b = '';
// Here we check if our $isResult are true or false
if (true === data.isResult) {
// Here came the .each() function in play
$.each(data.result, function(i, obj) {
a = '<li>' + obj.output_1 + '</li>' +
'<li>' + obj.output_2 + '</li>';
});
} else {
b = '<li>' + obj.output_3 + '</li>' +
'<li>' + obj.output_4 + '</li>';
}
// Return your result
return a + b;
}
$(document).ready(function() {
// I pick up your search function
function search() {
var query_value = $('input#search').val();
// Ajax call
$.ajax({
type: 'POST',
cache: false,
dataType: 'json', // <== Notice the datatype:
// For transporting JSON-formatted data,
// which can include strings, arrays, and objects
data: { query: query_value },
beforeSend: function() { /* some loader */ }
}).done(function(json) {
// Putting result from function getResult() into your HTML
$('ul#results').html(getResult(json));
}).fail(function() { /* some error handling */ });
return false;
}
// Thats your original code, except that I change the .live() to .on()
$("input#search").on("keyup", function(e) {
// Set Timeout
clearTimeout($.data(this, 'timer'));
// Set Search String
var search_string = $(this).val();
// Do Search
if (search_string == '') {
$("ul#results").fadeOut();
$('h4#results-text').fadeOut();
}else{
$("ul#results").fadeIn();
$('h4#results-text').fadeIn();
$(this).data('timer', setTimeout(search, 100));
};
});
});
请记住,这只是一个例子!
您可以阅读此内容以获取更多信息: