如何向搜索结果添加链接


How Do You Add A Link To A Search Results

我编写这段代码是为了能够搜索我的数据库,然后显示结果。(代码复制如下)

我需要一些帮助,因为我需要知道如何从搜索结果中创建链接,这样他们就可以点击结果,它会自动在新网页上打开详细信息。

我是PHP的新手,所以任何人能提供的帮助都是非常感激的。

感谢

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
     $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
     if($_POST['filter1'] == "Whole Site"){
         $sqlCommand = "(SELECT id, title AS title FROM cars WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%') UNION (SELECT id, title AS title FROM motorcycles WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%')";
     } else if($_POST['filter1'] == "cars"){
         $sqlCommand = "SELECT id, title AS title FROM cars WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%'";
     } else if($_POST['filter1'] == "motorcycles"){
         $sqlCommand = "SELECT id, title AS title FROM motorcycles WHERE title LIKE '%$searchquery%' OR description LIKE '%$searchquery%'";
     }
        include_once("testconn2.php");
        $query = mysql_query($sqlCommand) or die(mysql_error());
     $count = mysql_num_rows($query);
     if($count > 1){
         $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
         while($row = mysql_fetch_array($query)){
                 $id = $row["id"];
            $title = $row["title"];
            $search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';
                } // close while
     } else {
         $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
     }
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Database </h2>   <h4><a href="Sign in.php">Sign in</a> <a href="UserRegistration.php">Register</a></h4>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
Within: 
<select name="filter1">
<option value="Whole Site">Whole Site</option>
<option value="Cars">Cars</option>
<option value="Motorcycles">Motorcycles</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

这里似乎已经有了搜索结果的链接,对吗?

 $search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';

您可能想删除$_SERVER['HP_SELF']或将其移动到item.PHP之前,因为它会将其附加到您的文件名中,我确信您不希望这样做。

现在,您所要做的就是创建item.php页面,并使用$_GET['id']来获取您要查找的项目的id。

一旦你得到了你要找的项目,你就可以对你的数据库进行查询。

您还应该考虑使用PDO或mysqli,而不是常规的mysql命令,因为现在不建议使用它们。

为了获得参考,您可以尝试w3schools或PHP的手册。

希望这能有所帮助!

将表单方法替换为"get"而不是"post",然后在提交表单时,它将生成您要查找的链接。

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

链接将看起来像:

http://yourwebsite/yourfile.php?searchquery=sometext&fillter1=somevalue1&myBtn=submit
<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '" taget="_blank">' .$title . '</a><br>';

我只使用"_blank"目标,并在链接页面中使用$_GET变量来显示您的结果。

$search_output .= '<a href="item.php"' .$_SERVER['PHP_SELF'] . '"?c=$catId&p=' . $id . '">' .$title . '</a><br>';

由于$_SERVER['PHP_SELF']包含在双引号之外,看起来它会产生类似的东西

<a href="item.php"search.php"?c=1&p=1">Title</a><br>

更有可能你只是想要(哦,并编码你的安培数)

$search_output .= '<a href="item.php?$catId&amp;p=$id">$title</a><br>';