dpdbu24262 2015-04-15 23:38
浏览 65

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

Hey I got problem with my(found) search code. If I search for something that is split into two columns like Name and Last name, it only works if the first name is only One name(word). For example If I got a Name column with two names such as Nancy Dandry, and a LastName column with 5 names, Tomson Craig John McGee Name, I can search for it only if I use one name(word) from the Name Column.

Example

:Search Field: Nancy John McGee Name = Results

:Search Field: Dandry Tomson John Tomson = Results

:Search Field: Nancy Dandry = No Results

:Search Field: Nancy Dandry Tomson = No Results.

Basically if the Name column has two or more words and I search for two or more words, I get no results at all. How do I got about fixing this? I really can't find anyway, I've searched all over... I believe the Culprit is the little snippet of code below, but I'm not sure.

I've been trying to tackle this issue several times for a few months and given up on it every time :& I've tried all the other options, but this is the closest I've been to solving it... Sounds like I'm building a rocket, but yeah... :)

 $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%'";

}

The Form I'm using

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

Full Php Code

<?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条回答 默认 最新

  • duanpin5168 2015-04-16 00:02
    关注

    You could try a fulltext index across (first_name, last_name), eg:

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

    and then query against it like so:

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

    All of which will return matching rows, whereas:

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

    Will not return any matches, because the + signs in front of the name elements will ensure that only results that match all those word are included. heres an example

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向