0
<?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 变量的任何地方加粗。这可能吗?

谢谢

4

2 回答 2

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 或 PDO,因为强烈建议不要使用 mysql 扩展

于 2012-09-11T11:26:51.583 回答
0

您可以在每个 Print 语句中使用类似的内容吗?

str_replace($kword, "<b>$kword</b>", $info[...])

(或 CSS 例如 <span style='font-weight:bold'>...</span> 如果您愿意)。

于 2012-09-11T11:30:13.393 回答