我有点卡在一些我不太熟悉的事情上。
对于我的搜索栏,当用户输入内容并有结果时,我希望该特定结果链接到特定页面(如果可能的话)。
以下是我的代码中的内容:
<?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 >
$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
,但我不确定如何继续。任何帮助将不胜感激:)
答案 0 :(得分:0)
这是因为您的代码方向不正确..这就是......
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;
}
?>