简体   繁体   English

在整个页面中用PHP粗化特定变量

[英]Bolding a specifc variable in PHP throughout a page

<?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>

I am relatively new to PHP, and i was curious as to whether a certain function is possible. 我对PHP比较陌生,对于是否可以使用某些功能感到好奇。 I have a keyword Search and the code for the results page is above. 我有一个关键字Search,结果页面的代码在上面。 I would like to bold wherever the $kword variable appears on the page. 我想将$ kword变量出现在页面上的任何地方加粗。 is this possible? 这可能吗?

Thanks 谢谢

You can create a function to do so, and call it prior to echo'ing the variables. 您可以创建一个函数来执行此操作,并在回显变量之前调用它。

Instead of: $info['question'] use boldText($info['question'], $kword) 代替: $info['question']使用boldText($info['question'], $kword)

function boldText($text, $keyword) {
    return str_ireplace($keyword, "<strong>$keyword</strong>", $text);
}

As a side note, don't forget to escape $kword with mysql_real_escape_string() before using it in a SQL query, or even better, consider using MySQLi or PDO since mysql extension is strongly discouraged 附带说明一下,在SQL查询中使用$kword之前,请不要忘记使用mysql_real_escape_string()来转义$kword ,或者甚至更好,请考虑使用MySQLi或PDO,因为强烈建议不要使用mysql扩展

Can you use something like this in each of your Print statements? 您可以在每个Print语句中使用类似的内容吗?

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

(or CSS eg <span style='font-weight:bold'>...</span> if you prefer). (如果愿意,可以使用CSS,例如<span style ='font-weight:bold'> ... </ span>)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM