使搜索结果可链接?

时间:2012-10-31 17:00:45

标签: php search hyperlink

我已经创建了这个小搜索脚本来定位用户并尝试使搜索结果可以点击。我试图将网址指向个人资料ID,但没有太多运气。基本上每个配置文件的ID都是1,2,3,4等,我需要找到一种方法,使搜索结果与点击时的配置文件ID相对应。有人可以帮忙吗?

<form method="get" action="">
<input type="text" name="query" class="search" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/loginarrow1.png" class="searchbutton" name="submit" value="Start Search" />
</form>

<?php
//PHP CODE STARTS HERE

if(isset($_GET['submit'])){

// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="database";
$db_tb_name="ptb_profiles";
$db_tb_atr_name="display_name";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("SELECT * FROM `$db_tb_name` 
        WHERE display_name like '%".$query."%' 
        OR location LIKE '%".$query."%' OR orientation LIKE '%".$query."%'");
echo "<div class=\"results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<div class=\"spacing\"><a href=\"profile.php?id={$profile['user_id']}\">";
    echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div>";

}


mysql_close();
}

?>

我试图使用这个功能,但它只列出了所有用户,基本上不是我想要的。

function get_searched_users() {
            global $connection;
            $query = "SELECT *
                        FROM ptb_users, ptb_profiles
                        WHERE ptb_users.account_type = \"user\"
                        AND ptb_users.account_status = \"Active\"
                        AND ptb_profiles.user_id = ptb_users.id
                        AND display_name like '%".$query."%' 
        OR location LIKE '%".$query."%' OR age LIKE '%".$query."%'";
            $search_set = mysql_query($query, $connection);
            confirm_query($search_set);
            return $search_set;

    }

1 个答案:

答案 0 :(得分:0)

试试这个:

while ($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<div class=\"spacing\">";
    echo "<a href=\"profile.php?id={$data_fetch['user_id']}\">";
    echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div>";
}

实际上,您需要使用$data_fetch变量并使用$profile变量。