点击它们时无法选择建议

时间:2014-01-03 14:04:52

标签: php html css search-suggestion

点击它们时,我无法选择任何建议。

我希望在我悬停时可以点击,选择和更改颜色。

我该怎么做?

这是我目前的代码:

<?php
    include 'connect.php'; //connect with database
    $query = $_GET["q"];
    if($query != "")
    {
        $safequery = mysqli_real_escape_string($con,$query);
        $stmt = "SELECT * FROM searchengine WHERE title LIKE '%" . $safequery . "%' OR keywords LIKE '%" . $safequery . "%' OR link LIKE '%" . $safequery . "%' LIMIT 4";
        $result = mysqli_query($con,$stmt) or die(mysqli_error($con));

        $number_of_result = mysqli_num_rows($result);
        if ($number_of_result > 0)
        {
            //results found here and display them
            while ($row = \mysqli_fetch_assoc($result))
            { //show first 10 results
                //add $title to an array which you will call json_encode(arr) on.
                $title = $row["title"];
                echo "<div id='sugg-search-result'>";
                echo "<div id='sugg-title'>" . $title . "</div>";
                echo "</div>";
            }
        }
    }
?>

2 个答案:

答案 0 :(得分:0)

您可以尝试如下:

<?php

include 'connect.php'; //connect with database

$query = isset($_GET["q"]) ? $_GET : '' ;

if($query != "")
{
    $safequery = mysqli_real_escape_string($con,$query);

    $stmt = "SELECT * FROM searchengine WHERE title LIKE '%" . $safequery . "%' OR keywords LIKE '%" . $safequery . "%' OR link LIKE '%" . $safequery . "%' LIMIT 4";

    $result = mysqli_query($con,$stmt) or die(mysqli_error($con));

    $number_of_result = mysqli_num_rows($result);

    if ($number_of_result > 0) {
        //results found here and display them
        while ($row = \mysqli_fetch_assoc($result)) { //show first 10 results
            //add $title to an array which you will call json_encode(arr) on.
            $title = $row["title"];
            echo "<div id='sugg-search-result'>";
            echo "<div class='suggestion' id='sugg-title'>" . $title . "</div>";
            echo "</div>";

        }

    }
}
?>

<强> CSS:

<style>
    .suggestion:hover {
        color:orange;
    }
</style>

<强>使用Javascript:

<!-- jQuery -->

<script>
    $(document).on('click','.suggestion',function(){
        // write your necessary javascript code
    });
</script>

答案 1 :(得分:0)

您需要使用JavaScript和CSS。 基本示例:

// HTML Content
<style type="text/css">
#sugg-search-result {
    width: 200px;
    height: 150px;
    /* ... */
}

#sugg-search-result .sugg-title {
    height: 13px;
    overflow: hidden;
    /* ... */
}

#sugg-search-result .sugg-title:hover {
    background: red;
    cursor: pointer;
}


#sugg-search-result .sugg-title:active {
    background: green;
}
</style>

//...

<?php
    include 'connect.php'; //connect with database
    $query = $_GET["q"];
    if($query != "")
    {
        $safequery = mysqli_real_escape_string($con,$query);
        $stmt = "SELECT * FROM searchengine WHERE title LIKE '%" . $safequery . "%' OR keywords LIKE '%" . $safequery . "%' OR link LIKE '%" . $safequery . "%' LIMIT 4";
        $result = mysqli_query($con,$stmt) or die(mysqli_error($con));

        if (mysqli_num_rows($result) > 0)
        {
            echo '<div id="sugg-search-result">';
            //results found here and display them
            while( ($row = mysqli_fetch_assoc($result)) !== false )
            { //show first 10 results
                //add $title to an array which you will call json_encode(arr) on.
                $title = $row['title'];
                echo '<div class="sugg-title" onclick="console.log(\'Make an action here\');">' , $title , '</div>';
            }
            echo '</div>';
        }
    }
?>