duan19911992 2012-08-02 22:47
浏览 31

如何添加链接到搜索结果

I have written this code to enable a search of my database and to then display the results. (code copied below)

I need some help as I need to know how to then create a link from the search results so they can click on the result and it automatically opens the details on a new webpage.

I am very new to PHP so any help anyone can give is appreciated.

Thanks

<?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>
  • 写回答

4条回答 默认 最新

  • doulai6469 2012-08-02 22:51
    关注

    Replace the form method with "get" instead of "post", then when the form is submitted it will generate the link you are looking for.

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

    The link will look like:

    http://yourwebsite/yourfile.php?searchquery=sometext&fillter1=somevalue1&myBtn=submit
    
    评论

报告相同问题?