dsv768456 2015-02-28 23:10
浏览 52
已采纳

搜索引擎有多个关键字

I have a search engine that is working , but only when I search 1 word. Whenever I search multiple keywords I only get 1 result.

Example : In my database I have tags like 'test' and 'hello' ; Whenever I enter "test hello" and click "Search" it displays :

1 result hello (this being the title of the post with the tag = hello).

My code (search.php - the page where I get the search results):

<?php

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

if(!$button) {
    echo "you didn't submit a keyword";
} else {
    if(strlen($search)<=1) {
        echo "Search term too short";
    } else {
        echo "You searched for <b>$search</b> <hr size='1'></br>";
        mysql_connect("localhost","root","root");
        mysql_select_db("myschool");

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

        foreach($search_exploded as $search_each) {
            $x = NULL;
            $construct = NULL;
            $x++;
            if($x==1) {
                $construct .="tag LIKE '%$search_each%'";
            } else {
                $construct .="OR tag LIKE '%$search_each%'";
            }

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

            $foundnum = mysql_num_rows($run);

            if ($foundnum==0) {
                echo "Sorry, there are no matching result for <b>$search</b>.";
            } else {
                echo "$foundnum results found !<p>";

                while($runrows = mysql_fetch_assoc($run)) {
                    $title = $runrows ['title'];
                    $tag = $runrows ['tag'];

                    echo "<a href='#'><b>$title</b></a><br>";

                }
            }

        }
    }
}
?>

Problem is probably around the $x=++ part , because I believe the engine is not displaying or even searching through all the rows in the database , and not displaying when the num row count > 1 .

Thanks in advance guys.

EDIT :

I now get multiple results with the code above BUT I get them in this form :

You searched for hello test postare

1 results found ! HELLO 1 results found !

Test 1 results found !

postare noua

How can I make it add the results in 1 place , and not say it everytime it finds a new result for a different keyword ?

  • 写回答

2条回答 默认 最新

  • doumen1883 2015-02-28 23:40
    关注

    You need to start $x variable before foreach statement, and dont set it as null if you want to use it as an integer.

    The $construct variable has the same error, you must be having the same response for three times, thats because you have to close the foreach statement before calling mysql select.

    $x = 1;
    $construct = NULL;
    
    foreach($search_exploded as $search_each) 
    { 
        if($x==1) { 
            $construct .="tag LIKE '%$search_each%'"; 
        } else { 
            $construct .="OR tag LIKE '%$search_each%'"; 
        } 
        $x++;
    }
    $select ="SELECT * FROM posts WHERE $construct";
    ...
    

    Last Edit

    <?php
    
    $button = $_GET ['submit'];
    $search = $_GET ['search']; 
    
    if(!$button) {
        echo "you didn't submit a keyword";
    } else {
        if(strlen($search)<=1) {
            echo "Search term too short";
        } else {
            echo "You searched for <b>$search</b> <hr size='1'></br>";
            mysql_connect("localhost","root","root");
            mysql_select_db("myschool");
    
            $search_exploded = explode (" ", $search);
            $x = 1;
            $construct = '';
    
            foreach($search_exploded as $search_each) {
                if($x==1) {
                    $construct .="tag LIKE '%$search_each%'";
                } else {
                    $construct .="OR tag LIKE '%$search_each%'";
                }
                $x++;
            }
    
            $select ="SELECT * FROM posts WHERE $construct";
            $run = mysql_query($select);
    
            $foundnum = mysql_num_rows($run);
    
            if ($foundnum==0) {
                echo "Sorry, there are no matching result for <b>$search</b>.";
            } else {
                echo "$foundnum results found !<p>";
    
                while($runrows = mysql_fetch_assoc($run)) {
                    $title = $runrows ['title'];
                    $tag = $runrows ['tag'];
    
                    echo "<a href='#'><b>$title</b></a><br>";
    
                }
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号