搜索引擎显示重复的搜索结果

时间:2015-11-19 05:41:58

标签: php mysql

我正在尝试做一个搜索引擎项目并在一个单独的测试页面上成功完成了它。但是,当我尝试将其集成到我原始的网页设计中时,我遇到了问题。

这是我所指的测试页面,代码处理正常,显示的结果基于"大学"关键字。

Search Engine

这是我原创的网页设计,我在其中添加了搜索引擎的PHP代码。输出如图所示。我使用完全相同的代码,并连接到同一个数据库。但是,当我搜索相同的关键字时,在这种情况下是" university",我从这两个页面得到不同的输出。

Video Results

这是我的搜索引擎脚本:

<?php
$servername = "localhost";
$username   = "root";
$password   = "null";
$database   = "seetoh88_mp16";

mysql_connect("$servername","$username",""); //Procedural style
mysql_select_db("$database");

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

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

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

// An array for the `LIKE` conditions
$construct_list = [];

// Adding the conditions in the array
foreach($search_exploded as $search_each){
    $construct_list[] = "keywords LIKE '%$search_each%'";
}

// Joining them using OR
$construct = implode(" OR ", $construct_list);

// Supposing there are no keywords, the
// WHERE should not exist. So make a separate var for that - 
//$where_clause = "";
//if(trim($construct) != ""){
$where_clause = "WHERE $construct";
//}

// Perform your query
$query ="SELECT * FROM video INNER JOIN keyword
        on keyword.videoID = video.videoID
        $where_clause";

$runquery = mysql_query($query);

if ($runquery === false) {
    echo mysql_error();
}

$foundnum = mysql_num_rows($runquery);

if ($foundnum === false) {
    echo mysql_error();
}

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($runquery)){
        $videoID= $runrows ['videoID'];
        $thumbnail = $runrows ['thumbnail'];
        $title = $runrows ['title'];
        $description = $runrows ['description'];
        $keywords = $runrows ['keywords'];
        $embed_url = $runrows ['embed_URL'];

        echo "<a href='$embed_url'><b>$title</b></a><br>
        $description<br>
        <a href='$embed_url'>$embed_url</a><p>";
        echo "<a href='video-page.php'> <img src='$thumbnail' alt='' width='350px' height='200px'>";
    }
}

原创网页设计

<div class="container bottom">
    <div class="col-sm-3">
        <div class="portfolio-item classic">
            <div class="portfolio-item-thumbnail">
<?php 
$x = 1;
while($x<=3){
    echo "<a href='video-page.php'> <img src='$thumbnail' alt='' width='350px' height='200px'><hr>";
    $x++;
}
?>          </div><!-- portfolio-item-thumbnail -->
        </div><!-- col -->
    </div>
    <div class="col-sm-8"> 
        <div class="portfolio-item-description">
<?php
$y = 1;
while($y<=3){
    echo "<h4>$title</h4>";
    echo "<p>$description</p>";
    $y++;
}
?>      </div><!-- portfolio-item-description -->
    </div><!-- col -->
</div>

我想指出我使用了一个while循环来显示从数据库中检索到的$thumbnail$description。有人可以帮我解决这个问题吗?非常感谢!

0 个答案:

没有答案
相关问题