搜索多列,第一列中的多个单词

时间:2015-04-15 23:38:43

标签: php mysql search

嘿,我的(找到的)搜索代码出了问题。 如果我搜索的内容分为两列,如姓名和姓氏, 只有当名字只有一个名字(单词)时它才有效。 例如,如果我有一个名称列,有两个名字,如南希丹德里, 还有一个包含5个名字的LastName列,Tomson Craig John McGee Name, 只有当我使用名称栏中的一个名字(单词)时,我才能搜索它。

示例

:搜索字段: Nancy John McGee名称= 结果

:搜索字段: Dandry Tomson John Tomson = 结果

:搜索字段: Nancy Dandry = 没有结果

:搜索字段: Nancy Dandry Tomson = 没有结果。

基本上,如果“名称”列有两个或更多单词,并且我搜索两个或更多单词,则根本没有结果。 我该如何解决这个问题?无论如何我真的无法找到,我已经搜遍了所有... 我相信Culprit是下面的一小段代码,但我不确定。

我几次试图解决这个问题好几个月并且每次都放弃了这个问题:&我已经尝试了所有其他选项,但这是我最接近解决它的方法......听起来我正在建造一个火箭,但是是啊......)

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

$x = "";
$construct = "";  

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

}

表格I使用

<form action='sear.php' method='GET'>
                            <center>
                                    <h1> Search</h1>
                                            <input type='text' size='50' name='search'></br></br>
                                    <input type='submit' name='submit' value='Search' ></br></br></br>
                            </center>
                    </form>

完整的Php代码

<?php

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

if(strlen($search)<=1)
echo "Search term is too short!";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","Name_Name","Password");
mysql_select_db("Database_Test");
    $output = ' ' ;
$search_exploded = explode (" ", $search);

$x = "";
$construct = "";  

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




}

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

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Excuse for not finding the result... None";
else
{ 

echo "$foundnum results found !<p>";

$per_page = 10;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0; 
$getquery = mysql_query("SELECT id, Name, LastName FROM Information WHERE $construct ORDER BY id DESC LIMIT $start, $per_page");

while($runrows = mysql_fetch_assoc($getquery))
{
$id = $runrows ['id'];
$Name = $runrows ['Name'];
$LastName = $runrows ['LastName'];

$output .= ' <div class="Name">' . $Name . ' </div> <div class="LastName">' . $LastName . ' </div> ';



}

//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 href='sear.php?search=$search&submit=Search+names+keyword&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 href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&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 href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&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 href='sear.php?search=$search&submit=Search+names+keyword&start=0'>1</a> ";
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&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 href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}

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

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

//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$next'>Next</a> ";    
}   
echo "</center>";
} 
} 
?>

1 个答案:

答案 0 :(得分:0)

您可以尝试跨(first_name, last_name)的全文索引,例如:

alter table names add fulltext name (first_name, last_name);

然后像这样查询:

select * 
  from names 
    where match (first_name, last_name) 
      against('+Nancy +Dandry' in boolean mode);

select * 
  from names 
    where match (first_name, last_name) 
      against('+Nancy +Dandry +Tomson'in boolean mode);
select * 
  from names 
    where match (first_name, last_name) 
      against('+Dandry +Tomson +John +Tomson'in boolean mode);
select * 
  from names 
    where match (first_name, last_name) 
      against('+Nancy +John +McGee +Name'in boolean mode);

所有这些都将返回匹配的行,而:

select * 
  from names 
    where match (first_name, last_name) 
      against('+Nancy +Cylon'in boolean mode);

不会返回任何匹配项,因为名称元素前面的+符号将确保仅包含与所有这些单词匹配的结果。 heres an example