如何使搜索查询结果显示为其网页的链接 [英] how to make search query results show up as links to their pages

查看:116
本文介绍了如何使搜索查询结果显示为其网页的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php新手,你可以看到:)

I am new to php as you can see :)

如何让搜索查询结果显示为其网页的链接。我需要在我的数据库中添加页面表中的链接列吗?然后search_output出来?

how can i make the search query results show up as links to their pages. do i need to add to my database a 'links' column in my pages table? Then search_output them out?

    <?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>


推荐答案

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

You would need to append all the output from the query fetch, to your $search_output string.

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

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

但这也取决于链接的外观,它们可能不是如上所述,或者可能在外部链接。那么在这种情况下,是的,您输入链接的额外字段可能很有用。

But it also depends on how your links look as well, they might not be as simple as above, or might link externally. Then in that case, yes an extra field where you hardinput the link might be useful.

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

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

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

这篇关于如何使搜索查询结果显示为其网页的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆