user1662306
user1662306

Reputation: 502

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. I have a keyword Search and the code for the results page is above. I would like to bold wherever the $kword variable appears on the page. is this possible?

Thanks

Upvotes: 0

Views: 72

Answers (2)

Tchoupi
Tchoupi

Reputation: 14691

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)

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

Upvotes: 1

Steve Chambers
Steve Chambers

Reputation: 39454

Can you use something like this in each of your Print statements?

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

(or CSS e.g. <span style='font-weight:bold'>...</span> if you prefer).

Upvotes: 0

Related Questions