Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL/PHP搜索引擎-不刷新?提供的例子和文件_Php_Mysql_Database_Search Engine - Fatal编程技术网

MySQL/PHP搜索引擎-不刷新?提供的例子和文件

MySQL/PHP搜索引擎-不刷新?提供的例子和文件,php,mysql,database,search-engine,Php,Mysql,Database,Search Engine,我的问题很长,所以我马上开始。我设置了一个搜索引擎,它将数据GET方法发送到一个php脚本,该脚本查询MySQL数据库,然后显示结果。为了更好地了解项目,您可以在以下位置查看项目: 表单由一个搜索框和一个可缩小搜索范围的多选选项组成 一切都很好,但有一件事我想改变,但我不知道如何改变。如果有人能详细地解释,我需要做什么才能显示结果,我想它将在一个用HTML编写的div中?从与原始表单相同页面上的查询中删除,而无需刷新页面 如果您想要一个例子,这里有一个: 搜索保持在同一页上 另外,我知道mysq

我的问题很长,所以我马上开始。我设置了一个搜索引擎,它将数据GET方法发送到一个php脚本,该脚本查询MySQL数据库,然后显示结果。为了更好地了解项目,您可以在以下位置查看项目:

表单由一个搜索框和一个可缩小搜索范围的多选选项组成

一切都很好,但有一件事我想改变,但我不知道如何改变。如果有人能详细地解释,我需要做什么才能显示结果,我想它将在一个用HTML编写的div中?从与原始表单相同页面上的查询中删除,而无需刷新页面

如果您想要一个例子,这里有一个: 搜索保持在同一页上

另外,我知道mysql的函数已经过时了。请不要打扰我。我对这一点还是很陌生,mysql_uu函数非常简单、简单,足以满足我的需要。当我在中学时获得了进一步的编程经验,我可能会将其转换为PDO或MySQLi

HTML格式:

search.php


您需要使用AJAX来实现这一点。使用AJAX,您可以发送异步请求,而无需将页面加载到服务器

这就是谷歌和其他主要搜索引擎的工作原理。请在此处了解:

Ajax-异步JavaScript和XML

发件人:

使用Ajax,web应用程序可以在后台异步向服务器发送数据和从服务器检索数据,而不会干扰现有页面的显示和行为

可以使用XMLHttpRequest对象检索数据

尽管名称不同,但并不需要使用XML,而是经常使用JSON,请求也不需要是异步的

实现这一点最简单的方法可能是通过jQuery,一个JavaScript框架;简单地说,我指的是你这方面最少的工作:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    // This is the equivalent of the older $(document).ready() event
    $(function(){
        $('#search-form').on('submit', function(event){

            event.preventDefault(); // prevents form submission

            var data = $('#search-form').serialize();   // get all of the input variables in a neat little package
            var action = $('#search-form').attr('action');  // get the page to get/post to

            $.get(action, data, function(response, status, jqxhr){

                $('#target').html(response);

            });
        });
    });
</script>

在这里,我将一个id添加到您的表单中,以便直接将其作为目标。我还假设有一个id为target的HTML元素,我可以将其与响应HTML一起注入,我还假设它只是一个片段。

值得注意的是,mysqli_*函数与API级别的mysql_*函数并没有太大的不同,这也是为什么我们很多人都坚持不使用不推荐的库的原因之一……避免页面刷新的唯一方法是通过JavaScript。你对脚本有多熟悉?@Teison只是Javascript还是Ajax?@TiesonT。我理解。我知道这没什么不同。希望很快,我计划阅读MySQLi手册,但是现在我是否可以得到一些帮助呢?“我真的很感激你的每一点帮助。”铁山。我所做的唯一一个javascript是Codecademy上的javascript轨迹,我认为这是非常基本的。
<?php

$button = $_GET ['submit'];
$search = $_GET ['search'];

print $search;



//validating search term length and connecting to db
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b><em>$search</em></b> and ";
mysql_connect("fake","fake","fake");
mysql_select_db("quinterestdb");}


//validating search term for protection; if statement to avoid errors being displayed
if (strlen($search)>1){
mysql_real_escape_string($search);}


//exploding search with multiple words   
$search_exploded = explode (" ", $search); //creates array of all terms in search
foreach($search_exploded as $search_each) //loops through array
{
$x++;
if($x==1)
$construct .="Answer LIKE '%$search_each%'"; //if only one value in array
else
$construct .="AND Answer LIKE '%$search_each%'"; //for each multiple value in array




}
$cat = $_GET ['category']; //preparing array (multiple choices)
if (is_array($cat))
{
foreach($cat as $val) //
{
if($val=="%") //if no category is selected
continue;
else //split array choices (separated by ' and ,)
$comma_separated = implode("','", $cat);
$newvar = "AND Category IN('$comma_separated')"; //if category is chosen
}
} //ignore for now


$constructs ="SELECT * FROM tossupsdb WHERE $construct $newvar"; //completed search query





//quering the database; if statement to avoid errors being displayed
if (strlen($search)>1){
$run = mysql_query($constructs);}

print "$constructs"; //ignore for now



//number of results found; if statement to avoid errors being displayed
if (strlen($search)>1){
$foundnum = mysql_num_rows($run);}

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{   
echo " <span class='badge badge-info'> $foundnum </span>  results were found:<hr        size='5'>";

$per_page = 25; //preparing for pagination; results that appear per page
$start = $_POST['start']; //where to start results on page
$max_pages = ceil($foundnum / $per_page); //number of pages there will be
if(!$start) //starting at 0
$start=0; 
$getquery = mysql_query("SELECT * FROM tossupsdb WHERE $construct $newvar LIMIT $start,        $per_page");

while($runrows = mysql_fetch_array($getquery)) //fetching results
{
$answer = $runrows ['Answer']; //obtaining individual data from database
$category = $runrows ['Category']; //obtaining individual data from database
$num = $runrows ['Question #'];  //obtaining individual data from database
$difficulty = $runrows ['Difficulty'];  //obtaining individual data from database
$question = $runrows ['Question'];  //obtaining individual data from database
$round = $runrows ['Round'];  //obtaining individual data from database
$tournament = $runrows ['Tournament'];  //obtaining individual data from database
$year = $runrows ['Year'];  //obtaining individual data from database

//what will be displayed on the results page 
echo "<div class='alert alert-info' style='border-radius: 20px'><div style='padding:    10px'>
<span class='label label-info' font-size='30px'><em>Tournament | Year | Round | Question # | Category</em></span></div>
<b>$tournament |</b> <b>$year |</b> <b>$round |</b> <b>$num |</b> <b>$category</b>
<p><em>Question:</em> $question</p>
<div class='alert alert-info'><em><strong>ANSWER:</strong></em> $answer</div></div><hr>
";

}


    //Pagination Starts
    echo "<center>";

$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $max_pages - 1;

if($max_pages > 1)
{   
//previous button
if (!($start<=0)) 
echo " <a class='btn btn-primary btn-large' href='search.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    

//pages 
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;   
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;                 
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))        
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
} 
$i = $i + $per_page;                                       
}

}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                 
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}

}
//close to end; only hide early pages
else
{
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a class='btn' href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
} 
$i = $i + $per_page;              
}
}
}

//next button
if (!($start >=$foundnum-$per_page))
echo " <a class='btn btn-primary btn-large' href='search.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
}   
echo "</center>";
} 

?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    // This is the equivalent of the older $(document).ready() event
    $(function(){
        $('#search-form').on('submit', function(event){

            event.preventDefault(); // prevents form submission

            var data = $('#search-form').serialize();   // get all of the input variables in a neat little package
            var action = $('#search-form').attr('action');  // get the page to get/post to

            $.get(action, data, function(response, status, jqxhr){

                $('#target').html(response);

            });
        });
    });
</script>