Jump to content

[SOLVED] Working on search engine! Possible to apply bold to Search String in results??


BigX

Recommended Posts

Hi,

 

I'm working on a search function right now! I got it up and running so that the organisation name of the companies in my databse and a short descrition about the organisation is shown if the search string is found in one of those two! Now i would like to have the words in the organisation name and the descritpion that match the search string to be shown in bold! Something like on the bottom of this page: http://www.adobe.com/devnet/dreamweaver/articles/php_blog3_06.html (by the way the code of my search engine isn't anything like the one in the link, this is just an example of the bold results)! Does anyone know how to do this?? This is my code of the search engine:

 

<?php 
if (isset($_GET['SearchString'])) {
  $string = $_GET['SearchString'];
}

include('cleaner.php');

$clean_string = new cleaner();
$cleaned_string = $clean_string->parseString($string);

$cleaned_string = strtolower($cleaned_string);
$split = split(" ",$cleaned_string);
foreach ($split as $array => $value) {
if (strlen($value) > 3) {
$new_string .= ''.$value.' ';
	}
}
$new_string=substr($new_string,0,(strLen($new_string)-1));

$split_stemmed = split(" ",$new_string);

$sql = "SELECT DISTINCT * FROM organisaties WHERE ("; 

while(list($key,$val)=each($split_stemmed)){
if($val<>" " and strlen($val) > 0){
$sql .= "(Organisatie LIKE '%{$val}%' OR Algemenebeschrijving LIKE '%{$val}%' OR Trefwoorden LIKE '%{$val}%') OR";
}
}
$sql=substr($sql,0,(strLen($sql)-3));//this will eat the last OR
$sql .= ") ORDER BY Organisatie ASC";

$query = mysql_query($sql) or die(mysql_error());
$row_sql = mysql_fetch_assoc($query);
$total = mysql_num_rows($query);

if($total>0) {

echo "<table width='450' cellspacing='0' border='0'>
<tr>
<th width='350' align='left' style='color: #CB0042' bgcolor='#FFFFFF'><div class='style11'><b>Resultaten voor: <span style='color:#000099'>{$_GET['SearchString']}</span></b></div></th>
</tr>
<tr>
<th width='350' style='color: #CB0042' bgcolor='#FFFFFF'><div class='style11'><b> </b></div></th>
</tr>";

while ($row_sql = mysql_fetch_assoc($query)) {

echo "<tr>";
  echo "<td style='font-size:16px'><div align='left'><a class='rood' href='organisatie.php?id={$row_sql['id']}'>" . $row_sql['Organisatie'] . "</a></div></td>";
    echo "</tr>";
echo "<tr>";
  echo "<td style='font-size:13px; color: #000000; border:1px solid #CB0042'><div align='justify'>" . $row_sql['Algemenebeschrijving'] . "</div></td>";
    echo "</tr>";
echo "<tr>";
  echo "<td style='font-size:16px'><div align='left'> </div></td>";
    echo "</tr>";

}
echo "</table>";

 ; 
} else
{
echo "Geen resultaten gevonden";
}
    ?>

 

Thanks in advance! Any help is appreciated!!

 

greetz

It gives you the function at the end of the page. Trying modifying something like this.

<?php
function myReplace($replace_this, $in_this)
{
return str_replace($replace_this, "<strong>$replace_this</strong>", $in_this);
}

$search = $_POST['search']; // the search parameter
$sql = "SELECT * FROM table WHERE field LIKE '%$search%'";
$result = mysql_query($sql) OR DIE (mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_array($result))
{
	echo "<div>".myReplace($search, $row['post_date'])."</div>";
	echo "<div>".myReplace($search, $row['post_title'])."</div>";
	echo "<div>".myReplace($search, $row['post_author'])."</div>";
	echo "<div>".myReplace($search, $row['post_content'])."</div>";
}
}
?>

Hey thanks,

 

after a little bit of puzzling and trying I figured it out! Below is the code now and this works fine! Thanks very much!!

 

<?php 
if (isset($_GET['SearchString'])) {
  $string = $_GET['SearchString'];
}

include('cleaner.php');

$clean_string = new cleaner();
$cleaned_string = $clean_string->parseString($string);

$cleaned_string = strtolower($cleaned_string);
$split = split(" ",$cleaned_string);
foreach ($split as $array => $value) {
if (strlen($value) > 3) {
$new_string .= ''.$value.' ';
	}
}
$new_string=substr($new_string,0,(strLen($new_string)-1));

$split_stemmed = split(" ",$new_string);

$sql = "SELECT DISTINCT * FROM organisaties WHERE ("; 

while(list($key,$val)=each($split_stemmed)){
if($val<>" " and strlen($val) > 0){
$sql .= "(Organisatie LIKE '%{$val}%' OR Algemenebeschrijving LIKE '%{$val}%' OR Trefwoorden LIKE '%{$val}%') OR";
}
}
$sql=substr($sql,0,(strLen($sql)-3));//this will eat the last OR
$sql .= ") ORDER BY Organisatie ASC";

$query = mysql_query($sql) or die(mysql_error());
$row_sql = mysql_fetch_assoc($query);
$total = mysql_num_rows($query);

if($total>0) {

echo "<table width='450' cellspacing='0' border='0'>
<tr>
<th width='350' align='left' style='color: #CB0042' bgcolor='#FFFFFF'><div class='style11'><b>Resultaten voor: <span style='color:#000099'>{$_GET['SearchString']}</span></b></div></th>
</tr>
<tr>
<th width='350' style='color: #CB0042' bgcolor='#FFFFFF'><div class='style11'><b> </b></div></th>
</tr>";

while ($row_sql = mysql_fetch_assoc($query)) {

echo "<tr>";
  echo "<td style='font-size:16px'><div align='left'><a class='rood' href='organisatie.php?id={$row_sql['id']}'>" . str_replace($string, "<strong>$string</strong>"
,$row_sql['Organisatie']) . "</a></div></td>";
    echo "</tr>";
echo "<tr>";
  echo "<td style='font-size:13px; color: #000000; border:1px solid #CB0042'><div align='justify'>" . str_replace($string, "<strong>$string</strong>"
,$row_sql['Algemenebeschrijving']) . "</div></td>";
    echo "</tr>";
echo "<tr>";
  echo "<td style='font-size:16px'><div align='left'> </div></td>";
    echo "</tr>";

}
echo "</table>";

 ; 
} else
{
echo "Geen resultaten gevonden";
}
    ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.