2014-10-28 81 views
0

我目前有一个有点工作的搜索栏,它的工作方式我想要它,我只是想知道是否有可能添加链接到不同的项目搜索?现在任何搜索重定向到'Session.php'我能够例如,如果有人搜索'家'我能够显示结果,然后用户可以点击'Home.php'?谢谢!如何将搜索栏中的链接转到其他页面?

的search.php代码:

<?php 
//--- Authenticate code begins here --- 
session_start(); 
//checks if the login session is true 
if(!isset($_SESSION['sess_user'])){ 
header("location:index.php"); 
} 
$username = $_SESSION['sess_user']; 

// --- Authenticate code ends here --- 
?> 

<link rel="stylesheet" type="text/css" href="../css/style1.css"> 
<?php 
    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error()); 
    mysql_select_db("aha") or die(mysql_error()); 
?> 
<html> 
</html> 

注销

<head> 
    <title>Search results</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <link rel="stylesheet" type="text/css" href="style.css"/> 
</head> 
<body> 
<?php 
    $query = $_GET['query']; 


    $min_length = 3; 


    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

     $query = mysql_real_escape_string($query); 
     // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT * FROM articles 
      WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error()); 

     // * means that it selects all fields, you can also write: `id`, `title`, `text` 
     // articles is the name of our table 

     // '%$query%' is what I'm looking for, % means anything, for example if $query is Hello 
     // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

     if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

    while($results = mysql_fetch_array($raw_results)){ 
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 



echo "<a href='../pages/session.php'><h3>{$results['title']}</h3></a><p>{$results['text']}</‌​p>"; 



    } 


} 
    else{ // if there is no matching rows do following 
    echo ("<br><br>No results</br></br>"); 
} 

} 

else{ // if query length is less than minimum 
echo ("</br></br>Minimum length is</br></br> ".$min_length); 
} 

?> 


</body> 

<br> 
<br> 
<a class="btn btn-search" type="button" href="index.php" >Search Again</a> 
</br> 
</br> 




DB FOR ARTICLE: 

http://puu.sh/ctUUq/2f73509cf2.png

谢谢!

+0

你的意思是显示最近的搜索? – khandelwaldeval 2014-10-28 10:17:13

+0

@devaldcool喜欢,让我们说一个用户搜索'家'我希望它显示'家'并重定向到'Home.php'。所以等等 – Sarah 2014-10-28 10:19:09

+0

在下拉菜单中(如谷歌)? – khandelwaldeval 2014-10-28 10:19:55

回答

0

您必须将每个记录的页面名称存储在数据库中,然后使用查询检索它。

考虑你将其存储在一个名为“PAGE_NAME”你的SQL表列中,更改此行以指向正确的网页,根据用户的查询:

echo "<a href='../pages/{$results['page_name']}'><h3>{$results['title']}</h3></a><p>{$results['text']}</‌​p>"; 
+0

['page_name']}。php我在那里放置什么:(? – Sarah 2014-10-28 10:22:07

+0

@Sarah对于数据库的每一行,您必须存储.php文件的名称(例如'Home.php')。我编辑了一下答案,你最好在你的SQL字段中包含文件扩展名 – Bigood 2014-10-28 10:24:59

+0

谢谢你的帮助,我已经弄明白了,但是我得到了这个错误。注意:未定义的索引:page_name在D:\ xampp \ htdocs中\ wd6_osmanovic \ pages \ search.php on line 74 – Sarah 2014-10-28 10:26:35

相关问题