1

私の質問はかなり長いので、私はそれを直します。MySQLデータベースにクエリを実行して結果を表示するphpスクリプトにデータ(GETメソッド)を送信する検索エンジンをセットアップしました。理解を深めるために、ここでプロジェクトを表示できます: http://www.quinterest.org/testcategory/

このフォームは、検索ボックスと、検索を絞り込む複数選択オプションで構成されています。

すべてが素晴らしく機能しますが、変更したいことが 1 つありますが、その方法がわかりません。誰かが詳細に、元のフォームと同じページのクエリから結果を表示できるようにするために何をする必要があるかを説明できる場合 (HTML で記述された div になると思いますか?)ページを更新?

例が必要な場合は、次のとおりです。http://quizbowldb.com/search 検索は同じページにとどまります。

PS mysql_ 関数が古くなっていることは知っています。私に嫌がらせをしないでください。私はまだこれに非常に慣れていませんが、mysql_ 関数はシンプルで簡単で、必要なものには十分です。プログラミングの経験を積んだら (私はまだ中学生です)、PDO や MySQLi に変換するかもしれません。

HTML フォーム:

 <form action='search.php' method='GET' class="form-search">  
    <input type='text' placeholder="What would you like to learn about?" class="input-xxlarge search-query" id="inputInfo" name='search'><br></br>
    <input type='submit' class="btn btn-large btn-primary" name='submit' value='Search'></br>
    <select multiple="multiple" name='category[]'>
        <option value="%">All</option>
        <option>Literature</option>
        <option>History</option>
        <option>Science</option>
        <option>Fine Arts</option>
        <option>Trash</option>
        <option>Mythology</option>
        <option>Phylosophy</option>
        <option>Social Science</option>
        <option>Religion</option>
        <option>Geography</option>
    </select>
    </center>
</form>

検索.php

<?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>";
} 

?>
4

2 に答える 2

0

そのためには AJAX を使用する必要があります。AJAX を使用して、非同期要求 (ページをロードしない要求) をサーバーに送信します。

これが、Google やその他の主要な検索エンジンのしくみです。ここでそれについて学んでください:Ajaxチュートリアル

Ajax - Asynchronous JavaScript And XML

ウィキペディアの Ajax の定義から:

Ajax を使用すると、Web アプリケーションは、既存のページの表示や動作に干渉することなく、サーバーと非同期に (バックグラウンドで) データを送信したり、サーバーからデータを取得したりできます。

XMLHttpRequest オブジェクトを使用してデータを取得できます。

その名前にもかかわらず、XML の使用は必須ではなく (JSON が代わりによく使用されます)、リクエストは非同期である必要はありません。

于 2013-06-18T04:15:06.453 に答える