<?php
# session
session_start();
# check that session is set and is valid
if(!isset($_SESSION['login']))
{ header('Location: login.php');
}
?>
<body>
<div class="maincontainer">
<div class="keywordhead">
<div align="center"><img src="Images/keyword_title.png" width="243" height="56" /></div>
</div>
<div class="results">
<p>
<?php
$kword = $_POST["kword"];
function boldText($text, $kword) {
return str_replace($kword, "<strong>$kword</strong>", $text);
}
$testin1 = substr($kword,0,1);
if($testin1 == "") {
print "<strong>No Keyword or a Keyphrase Entered, Please return to the '<a href='keyword_search.php'>Keyword Search Page</a>'</strong>";
}
else {
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
}
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and keywords like '%$kword%';")
or die(mysql_error());
echo "<table border=0 cellpadding=10>";
echo "<tr align = center bgcolor=white>
<td><b>Company Name</b></td><td><b>Section</b></td><td><b>Question</b></td><td><b>Answer</b></td>" ;
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td width = 130px>".boldText($info['company_name'], $kword) . "</td> ";
echo "<td width = 60px>".boldText($info['section_name'], $kword) . " </td>";
echo "<td width = 300px>".boldText($info['question'], $kword) . " </td>";
echo "<td width = 600px>".boldText($info['answer'], $kword) . " </td></tr>";
}
echo "</table>";
?>
</p>
</div>
<div class="footer"><a href="logout.php"><br />
Logout</a> | <a href="index.php">Index</a> | <a href="keyword_search.php">Back</a></div>
</div>
</body>
</html>
我对PHP比较陌生,我很想知道某个功能是否可行。我有一个关键字搜索,结果页面的代码在上面。我想在页面上出现$ kword变量的地方加粗。这可能吗?
由于
答案 0 :(得分:1)
您可以创建一个函数来执行此操作,并在回显变量之前调用它。
而不是:$info['question']
使用boldText($info['question'], $kword)
function boldText($text, $keyword) {
return str_ireplace($keyword, "<strong>$keyword</strong>", $text);
}
作为旁注,在SQL查询中使用它之前不要忘记使用mysql_real_escape_string()转义$kword
,或者更好,请考虑使用MySQLi or PDO,因为强烈建议不要使用mysql扩展名
答案 1 :(得分:0)
你能在每个Print语句中使用这样的东西吗?
str_replace($kword, "<b>$kword</b>", $info[...])
(或CSS,例如 &lt; span style ='font-weight:bold'&gt; ...&lt; / span&gt; 如果你愿意的话。)