Jump to content

[SOLVED] highlight or bold search results


dsaba

Recommended Posts

hey I searched the forums here and also google and figured out the basic knowledge of implementing highlights in search results, over the terms that they searched, i'm just unsure what the best way is of implementing it with my code that displays search results

 

this is my code that i'm using for search results to display:

$searchquery = mysql_query("SELECT * FROM mainfilelist WHERE filename LIKE \"%$trimmedsearchterm%\" ORDER BY filedateadded DESC");
$resultcount = mysql_num_rows($searchquery);


switch ($resultcount)
{
  case "0" : 
echo "Sorry no results for your search, request a file here";
mysql_close();
     break;
  default :
$searchtableheader = <<<EOT
<table width="760px" align="center" cellpadding="0" cellspacing="2" class="main">
  <tr>
    <td class="content"><br />
        <div class="titulares style1">Results for:<em> $searchterm </em>($resultcount)</div>
        <table  border="0" cellpadding="0" cellspacing="1" align="center" width="760px">
        <tr>
          <td colspan="5"> </td>
        </tr>
        <tr>
          <td width="33%" class="similares">Name</td>
          <td width="12%" class="similares">Mirrors</td>
          <td width="21%" class="similares">Date</td>
          <td width="20%" class="similares">Type</td>
          <td width="14%" class="similares">Submitter</td>
        </tr>
EOT;
echo $searchtableheader;
/////////end display searchtableheader 
     while ($searchqueryrow = mysql_fetch_array($searchquery)) {
$filelink = $searchqueryrow['filename'];
$filetotalmirrors = $searchqueryrow['filetotalmirrors'];
$filegeneraltype = $searchqueryrow['filegeneraltype_en'];
$filedateadded = $searchqueryrow['filedateadded'];
$filesubmitter = $searchqueryrow['filesubmitter'];

str_replace( "$trimmedsearchterm", "<span>$trimmedsearchterm</span>", $filelink );

$searchtableinside = <<<EOT
        <tr>
          <td class="nombre">$filelink</td>
          <td class="nombre">$filetotalmirrors</td>
          <td class="fecha">$filedateadded</td>
          <td class="descargas">$filegeneraltype</td>
          <td class="descargas">$filesubmitter</td>
        </tr>
EOT;
echo $searchtableinside;
}
//end while function and display searchtableinside
mysql_close();
     break;

 

 

this is the code I found here on the forums, but i'm not sure how to implement this with my code:

<?php
   function formatSearch($text,$term) {
     return preg_replace("/{$term}/i",'<b>${0}</b>',$text);  //note the ${0} in the replacement term keeps the original string that matched the search term
   }

?>
[code]

So, in your code, you might use it something like:

[code]
<?php
$searchquery="select * from site where (name like'%$term%' or url like'%$term%' or `desc` like'%$term%') and credits > 0 and state = 'Enabled' order by credits DESC  limit $start,$resultperpage";

//echo $searchquery;

   $result=mysql_query($searchquery);

   if(mysql_num_rows($result)!=0){
     while ($row = mysql_fetch_assoc($result)) {
       echo " ".formatSearch($row['name'],$term)." <br /> ".formatSearch($row['desc'],$term)."   ";
     }
   }

?>

[/code][/code]

Link to comment
https://forums.phpfreaks.com/topic/40716-solved-highlight-or-bold-search-results/
Share on other sites

ok try this out

 

<?php
   function formatSearch($text,$term) {
     return preg_replace("/{$term}/i",'<b>${0}</b>',$text);  //note the ${0} in the replacement term keeps the original string that matched the search term
   }

$searchquery = mysql_query("SELECT * FROM mainfilelist WHERE filename LIKE \"%$trimmedsearchterm%\" ORDER BY filedateadded DESC");
$resultcount = mysql_num_rows($searchquery);


switch ($resultcount)
{
  case "0" : 
echo "Sorry no results for your search, request a file here";
mysql_close();
     break;
  default :
$searchtableheader = <<<EOT
<table width="760px" align="center" cellpadding="0" cellspacing="2" class="main">
  <tr>
    <td class="content"><br />
        <div class="titulares style1">Results for:<em> $searchterm </em>($resultcount)</div>
        <table  border="0" cellpadding="0" cellspacing="1" align="center" width="760px">
        <tr>
          <td colspan="5"> </td>
        </tr>
        <tr>
          <td width="33%" class="similares">Name</td>
          <td width="12%" class="similares">Mirrors</td>
          <td width="21%" class="similares">Date</td>
          <td width="20%" class="similares">Type</td>
          <td width="14%" class="similares">Submitter</td>
        </tr>
EOT;
echo $searchtableheader;
/////////end display searchtableheader 
     while ($searchqueryrow = mysql_fetch_array($searchquery)) {

$filelink = formatSearch($searchqueryrow['filename'], $trimmedsearchterm);
$filetotalmirrors = $searchqueryrow['filetotalmirrors'];
$filegeneraltype = $searchqueryrow['filegeneraltype_en'];
$filedateadded = $searchqueryrow['filedateadded'];
$filesubmitter = $searchqueryrow['filesubmitter'];

str_replace( "$trimmedsearchterm", "<span>$trimmedsearchterm</span>", $filelink );

$searchtableinside = <<<EOT
        <tr>
          <td class="nombre">$filelink</td>
          <td class="nombre">$filetotalmirrors</td>
          <td class="fecha">$filedateadded</td>
          <td class="descargas">$filegeneraltype</td>
          <td class="descargas">$filesubmitter</td>
        </tr>
EOT;
echo $searchtableinside;
}
//end while function and display searchtableinside
mysql_close();
     break;
?>

 

Ray

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>search results</title>

<style type="text/css">

<!--

.style1 {font-size: 20px}

.style5 {

font-size: 16px;

font-weight: bold;

}

.style7 {font-size: 36px}

.style8 {

color: #FFFFFF;

font-family: Arial, Helvetica, sans-serif;

}

-->

</style>

</head>

 

<body>

 

<?php

//searchresults.php page

$searchterm = $_POST['searchterm'];

 

$trimmedsearchterm = trim($searchterm);

 

// check for an empty string and display a message.

if ($trimmedsearchterm == "")

  {

  echo "<p>Please enter a search term...</p>";

  exit;

  }

 

 

$connect = mysql_connect("localhost", "asdgsdag", "asdfsdgasdf") or

                  die ("Hey loser, check your server connection.");

mysql_select_db("adsfsghg");

 

$searchquery = mysql_query("SELECT * FROM mainfilelist WHERE filename LIKE \"%$trimmedsearchterm%\" ORDER BY filedateadded DESC");

$resultcount = mysql_num_rows($searchquery);

 

 

switch ($resultcount)

{

  case "0" :

echo "Sorry no results for your search, request a file here";

mysql_close();

    break;

  default :

$searchtableheader = <<<EOT

<table width="760px" align="center" cellpadding="0" cellspacing="2" class="main">

  <tr>

    <td class="content"><br />

        <div class="titulares style1">Results for:<em> $searchterm </em>($resultcount)</div>

        <table  border="0" cellpadding="0" cellspacing="1" align="center" width="760px">

        <tr>

          <td colspan="5"> </td>

        </tr>

        <tr>

          <td width="33%" class="similares">Name</td>

          <td width="12%" class="similares">Mirrors</td>

          <td width="21%" class="similares">Date</td>

          <td width="20%" class="similares">Type</td>

          <td width="14%" class="similares">Submitter</td>

        </tr>

EOT;

echo $searchtableheader;

/////////end display searchtableheader

 

function formatSearch($text,$trimmedsearchterm) {

    return preg_replace("/{$trimmedsearchterm}/i",'<b>${0}</b>',$text);

  }

 

    while ($searchqueryrow = mysql_fetch_array($searchquery)) {

$filelink = formatSearch($searchqueryrow['filename'], $trimmedsearchterm

$filetotalmirrors = $searchqueryrow['filetotalmirrors'];

$filegeneraltype = $searchqueryrow['filegeneraltype_en'];

$filedateadded = $searchqueryrow['filedateadded'];

$filesubmitter = $searchqueryrow['filesubmitter'];

 

//str_replace( "$trimmedsearchterm", "<span>$trimmedsearchterm</span>", $filelink );

 

$searchtableinside = <<<EOT

        <tr>

          <td class="nombre">$filelink</td>

          <td class="nombre">$filetotalmirrors</td>

          <td class="fecha">$filedateadded</td>

          <td class="descargas">$filegeneraltype</td>

          <td class="descargas">$filesubmitter</td>

        </tr>

EOT;

echo $searchtableinside;

}

//end while function and display searchtableinside

mysql_close();

    break;

}

//end switch statement

$searchtablefooter = <<<EOT

      </table>

EOT;

echo $searchtablefooter;

echo $trimmedsearchterm;

 

?>

</body>

</html>

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.