php - 如何使我的搜索栏链接到页面

标签 php html mysql

我对一些我不太明白的事情有点困惑。

对于我的搜索栏,当用户输入内容并获得结果时,我希望该特定结果链接到特定页面(如果可能)。

这是我的代码中的内容:

<?php

//--- Authenticate code begins here ---
session_start();

// checks if the login session is true
if(!isset($_SESSION['username'])){
    header("location:index.php");
}

$username = $_SESSION['username'];

// --- Authenticate code ends here ---

include ('header.php'); ?> 
<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("wd1_vtec_0100348514") or die(mysql_error());

?>

<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 "<p><a href><h3>".$results['title']."</h3>".$results['text']."</p>";
            // posts results gotten from database(title and text) you can also show id ($results['id'])
        }

    }
    else{ // if there is no matching rows do following
        echo "No results";
    }

}
else{ // if query length is less than minimum
    echo "Minimum length is ".$min_length;
}

?>

</body>

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

</html>

<div style="float:right">  <a class="btn btn-danger logout" href="logout.php" > Logout</a> </div>

<div id="menu">
    <ul id="nav">
        <li><a href="home.php" target="_self" >Home</a></li>
        <li><a href="session1.php" target="_self" >Sessions</a>
            <ul>
                <li><a href="session1.php" target="_self" >Session 1</a></li>
                <li><a href="session2.php" target="_self" >Session 2</a></li>
                <li><a href="session3.php" target="_self" >Session 3</a></li>
                <li><a href="session4.php" target="_self" >Session 4</a></li>
                <li><a href="session5.php" target="_self" >Session 5</a></li>
                <li><a href="session6.php" target="_self" >Session 6</a></li>
                <li><a href="session7.php" target="_self" >Session 7</a></li>
                <li><a href="session8.php" target="_self" >Session 8</a></li>
                <li><a href="session9.php" target="_self" >Session 9</a></li>
                <li><a href="session10.php" target="_self" >Session 10</a></li>
                <li><a href="session11.php" target="_self" >Session 11</a></li>
                <li><a href="session12.php" target="_self" >Session 12</a></li>
                <li><a href="session13.php" target="_self" >Session 13</a></li>
                <li><a href="session14.php" target="_self" >Session 14</a></li>
            </ul>
        <li><a href="blog.php" target="_self" >Blog</a></li>


    </ul>
</div>

<?php include ('footer.php'); ?> 

我已添加<a href但我不确定如何继续。任何帮助将不胜感激:)

最佳答案

这是由于您的代码方向不正确..试试这个...

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 "<p><a href='".$results['link']."'><h3>".$results['title']."</h3>".$results['text']."</‌​p>";
    }

}
    else{ // if there is no matching rows do following
    echo "No results";
}

}

else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}

?>

关于php - 如何使我的搜索栏链接到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901228/

相关文章:

PHP time() 改变小时和秒而不是分钟?

php - 从特定的 .csv 文件插入表 - PHP 脚本

html - 带有页眉和页脚的 div 高度 100% - 没有技巧

html - Chrome 和 IE8 中的 CSS 悬停问题

MySQL 总 COUNT 的 SUM

mysql - 说明在JOIN语句中选择哪个表 "FROM"

php - 使用 mysql 结果从多维 PHP 数组构建树

php - 将不包含扩展名的文件上传到 wordpress?

javascript - 如何在没有 jQuery 验证插件且没有提交按钮的情况下验证 JavaScript 表单

php - 更改每个 session 的 MySQL 模式