MadFly Posted July 22, 2011 Share Posted July 22, 2011 Hi, I have the following code, which does a search in a MYSQL database. Now I would like to highlight the word that has been searched for in the results. I have heard I should try preg_replace, but to no avail, and that I must use it in the while statement, but I do not know where. I would really like for it to keep the table format and everything. I have a css file called style.css which ONLY configures the highlight colors and font weight for <span></span>. Will you please help me to get this right? <?php include("searchheader.html"); $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } // actual db is suppose to be archivedb mysql_select_db("dev", $con); $result = mysql_query("SELECT * FROM data WHERE `id` LIKE '%$_POST[searchitem]%' OR `archive` LIKE '%$_POST[searchitem]%' OR `leer_nommer` LIKE '%$_POST[searchitem]%' OR `klient_naam` LIKE '%$_POST[searchitem]%' OR `description` LIKE '%$_POST[searchitem]%' OR `gesluit` LIKE '%$_POST[searchitem]%' "); $num_rows = mysql_num_rows($result); echo "<center>"; echo "<a href='index.html'>Back</a>"; echo "<BR><BR>"; echo "<input type=button onclick='window.print()' value='Print'>"; echo "<BR><BR>"; echo "You searched for<span>$_POST[soekitem]</span>, and we found "; echo "<b><font size='15' color='red'>$num_rows</font></b>"; echo " entries in the database.</font>"; echo "<BR>"; echo "<table border='frame'> <tr> <th>Archive Number</th> <th>File Number</th> <th>Client Name</th> <th>RE:</th> <th>Closed</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['archive'] . "</td>"; echo "<td>" . $row['file_number'] . "</td>"; echo "<td>" . $row['client_name'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['closed'] . "</td>"; } echo "</table>"; echo "</center>"; echo highlight_string($_POST[searchitem]); mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/242640-highlight-search-results-in-table/ Share on other sites More sharing options...
teynon Posted July 22, 2011 Share Posted July 22, 2011 $row['archive'] = str_ireplace($_POST['searchitem'], "<div class='highlight'>{$_POST['searchitem']}</div>", $row['archive']); Do this for each variable inside the while loop. Link to comment https://forums.phpfreaks.com/topic/242640-highlight-search-results-in-table/#findComment-1246210 Share on other sites More sharing options...
MadFly Posted July 22, 2011 Author Share Posted July 22, 2011 Thanks! that worked like charm! Took me a while to realise I had to change the search item back to the correct thing, as I translated everything to english before posting here. And I replaced that <div class="highlight"> with <span> Now my search code looks like this: ... while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['archive'] = str_ireplace($_POST['soekitem'], "<span>{$_POST['soekitem']}</span>", $row['archive']) . "</td>"; echo "<td>" . $row['leer_nommer'] = str_ireplace($_POST['soekitem'], "<span>{$_POST['soekitem']}</span>", $row['leer_nommer']) . "</td>"; echo "<td>" . $row['klient_naam'] = str_ireplace($_POST['soekitem'], "<span>{$_POST['soekitem']}</span>", $row['klient_naam']) . "</td>"; echo "<td>" . $row['description'] = str_ireplace($_POST['soekitem'], "<span>{$_POST['soekitem']}</span>", $row['description']) . "</td>"; echo "<td>" . $row['gesluit'] = str_ireplace($_POST['soekitem'], "<span>{$_POST['soekitem']}</span>", $row['gesluit']) . "</td>"; } Link to comment https://forums.phpfreaks.com/topic/242640-highlight-search-results-in-table/#findComment-1246270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.