du127953 2014-07-23 03:14
浏览 51

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

I'm a little stuck on something which I don't quite get.

For my search bar, when a user inputs something and has results I'd like that specific result to link to a specific page if that's possible.

Here is what I have in my code:

<?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'); ?> 

I've added <a href but I am unsure how to continue. Any help would be appreciated :)

  • 写回答

1条回答 默认 最新

  • drmcm84800 2014-07-23 03:25
    关注

    This is due to the improper oreintation of your code ..try this...

    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;
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错