将搜索查询结果回显到新页面


echo out search query results to new page?

嗨,我有一个我正在使用的搜索脚本,基本上当用户输入查询(即伦敦或 5 岁的用户)时,结果会显示在找到多少结果中。我之所以将其限制为五个,是因为空间不足。

我正在尝试制作一个显示在搜索结果底部的按钮,上面写着查看更多类似的结果。这基本上会链接到一个新页面并回显出用户最初键入的查询,因此首先他们输入伦敦并显示五个用户,结果为伦敦,然后如果他们单击链接,伦敦的所有结果都显示在新页面上。

有谁知道我如何回显查询或做类似的事情,这将给我所需的效果。

这是我的代码:

<?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="";
$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 ptb_stats
                        WHERE display_name like '%".$query."%' OR location LIKE '%".$query."%' OR age LIKE '%".$query."%' OR nationality LIKE '%".$query."%' OR ethnicity LIKE '%".$query."%' OR hobbies LIKE '%".$query."%' OR station LIKE '%".$query."%' LIMIT 5");
echo "<div class='"search-results'">";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<div class='"text'"><a href='"profile.php?id={$data_fetch['user_id']}'" class='"search'">";
    echo "<div class='"spacing'"><img width=35px height= 30px src='"data/photos/{$data_fetch['user_id']}/_default.jpg'" class='"boxgridsearch'"/> "; 
     echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div></div>";
}
echo "<div class='"morebutton-search'"><a href='"search.php?to=echo '%".$query."%''" target='"_blank'" '">+ view more results</a></div>";

mysql_close();
}
?>

你有正确的想法,但你被困在货物崇拜的编程模式中。

1)"$db_host"毫无意义。构建一个新字符串只是为了完整地保存其他字符串? $db_host没有引号(与其他变量类似)就是所需要的。

2)echo是一种语言结构。它没有返回值,并且不能将其"结果"连接在一起。您还将 echo 嵌入到字符串中,阻止 PHP 将其视为输出指令:

echo "[...snip...]<a href='"search.php?to=%$query%'" target=[...snip...]";

就是所有需要的。您不必"断开"变量的双引号字符串。对于多行字符串回声,您确实应该使用 HEREDOC,这使得代码更加清晰。