0

如您所见,我是 php 新手 :)

如何使搜索查询结果显示为指向其页面的链接。我需要在我的页面表中添加一个“链接”列到我的数据库吗?然后search_output出来呢?

    <?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']);
    {
        $sqlCommand = "(SELECT id, page_title AS title FROM pages WHERE MATCH (page_title,page_body) AGAINST ('$searchquery'))";

    }
    include_once("db_connects.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 .= "";
        } // close while
    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
}
?>
<html>
<head>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 


</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>
4

1 回答 1

0

您需要将查询提取的所有输出附加到您的 $search_output 字符串。

 while($row = mysql_fetch_array($query)){

        $id = $row["id"];
        $title = $row["title"];
        $search_output .= "<a href='".$_SERVER['SERVER_NAME']."/".$title."'>".$title."</a><br>";
        }

但这也取决于您的链接的外观,它们可能不像上面那样简单,或者可能是外部链接。然后在这种情况下,是的,您在其中硬输入链接的额外字段可能很有用。

回答下一个问题的新编辑...

  while($row = mysql_fetch_array($query)){

        $id = $row["id"];
        $title = $row["title"];
        $link = $row["links"];
        $search_output .= "<a href='".$link."'>".$title."</a><br>";
        }
于 2013-04-04T01:58:15.497 回答