MySQL / PHP搜索脚本 - 设置搜索参数?

时间:2013-06-17 01:27:10

标签: php mysql database search-engine

我有一个包含很多琐事问题的MySQL表。每个条目(行)都有几列(ID,问题#,类别,难度,问题,答案)。我有一个搜索引擎设置,可以像这样查询数据库(HTML表单将搜索发送到查询数据库并检索信息的php脚本)

的search.php:

<?php

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

if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b><em>$search</em></b> and ";
mysql_connect("mydb","notmypony","ponyrocksagain#");
mysql_select_db("quinterestdb");}

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="Answer LIKE '%$search_each%'";
else
$construct .="AND Answer LIKE '%$search_each%'";

}

$constructs ="SELECT * FROM tossups WHERE $construct";
$run = mysql_query($constructs);

$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;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0; 
$getquery = mysql_query("SELECT * FROM tossups WHERE $construct LIMIT $start, $per_page");

while($runrows = mysql_fetch_assoc($getquery))
{
$answer = $runrows ['Answer'];
$category = $runrows ['Category'];
$num = $runrows ['Question #'];
$difficulty = $runrows ['Difficulty'];
$question = $runrows ['Question'];
$round = $runrows ['Round'];
$tournament = $runrows ['Tournament'];
$year = $runrows ['Year'];

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

}

我的目标是能够在搜索表单中添加一个下拉菜单,允许用户选择将结果限制为所选类别的类别。我不知道如何实现这一目标。任何帮助将不胜感激。我渴望学习。无论如何,如果您对查看项目感兴趣,可以使用here

0 个答案:

没有答案
相关问题