how to make search query results show up as links to their pages

送分小仙女□ 提交于 2019-12-11 18:11:10

问题


I am new to php as you can see :)

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>

回答1:


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.

NEW EDIT TO ANSWER NEXT QUESTION...

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

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


来源:https://stackoverflow.com/questions/15801173/how-to-make-search-query-results-show-up-as-links-to-their-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!