Solved

PHP MYSQL search engine script does not remove duplicate search results from the array

Posted on 2007-11-23
9
408 Views
Last Modified: 2013-12-12
I have found a script from the net to build a php search engine to search through my mysql db. I like this particular script because it allows me to search on multiple fields.
I have edited it to make it specific to my db.
if you search using one word then that it fine. Problem occurs when you are searching on multiple words.
I have ran a search on two words knowing that one word appears in the 'AdvertTitle' field of a particular record and the other appears in the 'AdvertDescription' field of the same record. problem is that it will display that record twice.
There appears to be some unique_array() code which claims that it removes the duplicate id's out the array but it doesn't seem to work when I do the above as described.

Hope you can help.
Thanks,
Rich
<?
require "connect.php";
//specify how many results to display per page
$limit = 2;
 
 
// Get the search variable from URL
  $var = @$_GET['q'] ;
//trim whitespace from the stored variable
  $trimmed = trim($var);  
//separate key-phrases into keywords
  $trimmed_array = explode(" ",$trimmed); 
 
// check for an empty string and display a message.
if ($trimmed == "") {
  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
  }
 
// check for a search parameter
if (!isset($var)){
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
  }
// Build SQL Query for each keyword entered 
foreach ($trimmed_array as $trimm){
      
// EDIT HERE and specify your table and field names for the SQL query
     $query = "SELECT * FROM advert WHERE AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ; 
     // Execute the query to  get number of rows that contain search kewords
     $numresults=mysql_query ($query);
     $row_num_links_main =mysql_num_rows ($numresults);
 
     // next determine if 's' has been passed to script, if not use 0.
     // 's' is a variable that gets set as we navigate the search result pages.
     if (empty($s)) {
         $s=0;
     }
 
      // now let's get results.
      $query .= " LIMIT $s,$limit" ;
      $numresults = mysql_query ($query) or die ( "Couldn't execute query" );
      $row= mysql_fetch_array ($numresults);
 
      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
      do{
 //EDIT HERE and specify your field name that is primary key
          $adid_array[] = $row['AdvertId'];
      }while( $row= mysql_fetch_array($numresults));
 } //end foreach
 
if($row_num_links_main == 0 && $row_set_num == 0){
   $resultmsg = "<p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ;
}
   //delete duplicate record id's from the array. To do this we will use array_unique function
      $tmparr = array_unique($adid_array); 
   $i=0; 
   foreach ($tmparr as $v) { 
       $newarr[$i] = $v; 
       $i++; 
 } 
 
// now you can display the results returned. But first we will display the search form on the top of the page
?>
 
<form action="searchresults.php" method="get" name="search">
  <div align="center">
      <input name="q" type="text" value="<?php echo $q; ?>" size="15"> 
      <input name="search" type="submit" value="Search">
  </div>
</form>
 
<?php
// display what the person searched for.
 if( isset ($resultmsg)){
  echo $resultmsg;
  exit();
 }else{
  echo "Search results for: " . $var;
 }
 
foreach($newarr as $value){
 
// EDIT HERE and specify your table and field names for the SQL query
//$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
 $num_value=mysql_query ($query_value);
 $row_linkcat= mysql_fetch_array ($num_value);
 $row_num_links= mysql_num_rows ($num_value);
 
//now let's make the keywods bold. To do that we will use preg_replace function. 
//EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]
//This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line. 
  $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertTitle' ] );
  $deschigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertDescription' ] );
  $service1high = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertService1' ] );
 
foreach($trimmed_array as $trimm){
    if($trimm != 'b' ){
//IF you added more fields to search make sure to add them below as well.
        $titlehigh = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $titlehigh);
        $deschigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $deschigh);
        $service1high = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $service1high); 
     }
//end highlight
 
?>
 <p>
<?php echo $titlehigh; ?><br>
<?php echo $deschigh; ?><br>
<?php echo $service1high; ?>
</p>
 
<?php 
}  }  //end foreach $trimmed_array 
   if($row_num_links_main > $limit){
   // next we need to do the links to other search result pages
      if ($s>=1) { // do not display previous link if 's' is '0'
        $prevs=($s-$limit);
         echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
      }
     // check to see if last page
     $slimit =$s+$limit;
       if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
     // not last page so display next link
          $n=$s+$limit;
           echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
       
    }
}  //end foreach $newarr
?>

Open in new window

0
Comment
Question by:long405
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 17

Expert Comment

by:houssam_ballout
ID: 20341091
First try to print out the ID of each result
use also num_rows($query result) to see how many record its displaying

Can you try it on localhost? I'd tried searching records on same table from 2 fields containing the same word and it displays only one record
0
 

Author Comment

by:long405
ID: 20341170
I am running this on localhost using test data. There's not a problem with searching for one word where it will appear in both search fields (it will not repeat the same record twice) the problem occurs when I type two words into the search (see example below using the search term -  plumber bathroom). It will then bring back the record repeated twice. (in the below example, there are two records bought back (fine, as they both contain the search terms) but both are repeated (not right)).

I've added in the ID and also the num rows but I think I have the num rows syntax wrong as it is only saying that 1 row has ben returned.
********************************************************
Search results for: plumber bathroom

Advert Id: 651
AdvertTitle: Plumber A
AdvertDescription: Local Plumber for Plumbing & Heating Contractors - All Plumbing - Rhones - Guttering & Lead WorkCORGI Registered / SNIPEF Member - Established 1953 - Bathroom Installation Specialist


Advert Id: 651
AdvertTitle: Plumber A
AdvertDescription: Local Plumber for Plumbing & Heating Contractors - All Plumbing - Rhones - Guttering & Lead WorkCORGI Registered / SNIPEF Member - Established 1953 - Bathroom Installation Specialist


Advert Id: 652
AdvertTitle:Plumber B
AdvertDescription: Plumbing bathrooms etc


Advert Id: 652
AdvertTitle: Plumber B
AdvertDescription: Plumbing bathrooms etc

Num Rows: 1
<?
require "connect.php";
//specify how many results to display per page
$limit = 2;
 
 
// Get the search variable from URL
  $var = @$_GET['q'] ;
//trim whitespace from the stored variable
  $trimmed = trim($var);  
//separate key-phrases into keywords
  $trimmed_array = explode(" ",$trimmed); 
 
// check for an empty string and display a message.
if ($trimmed == "") {
  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
  }
 
// check for a search parameter
if (!isset($var)){
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
  }
// Build SQL Query for each keyword entered 
foreach ($trimmed_array as $trimm){
      
// EDIT HERE and specify your table and field names for the SQL query
     $query = "SELECT * FROM advert WHERE AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ; 
     // Execute the query to  get number of rows that contain search kewords
     $numresults=mysql_query ($query);
     $row_num_links_main =mysql_num_rows ($numresults);
 
     // next determine if 's' has been passed to script, if not use 0.
     // 's' is a variable that gets set as we navigate the search result pages.
     if (empty($s)) {
         $s=0;
     }
 
      // now let's get results.
      $query .= " LIMIT $s,$limit" ;
      $numresults = mysql_query ($query) or die ( "Couldn't execute query" );
      $row= mysql_fetch_array ($numresults);
 
      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
      do{
 //EDIT HERE and specify your field name that is primary key
          $adid_array[] = $row['AdvertId'];
      }while( $row= mysql_fetch_array($numresults));
 } //end foreach
 
if($row_num_links_main == 0 && $row_set_num == 0){
   $resultmsg = "<p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ;
}
   //delete duplicate record id's from the array. To do this we will use array_unique function
      $tmparr = array_unique($adid_array); 
   $i=0; 
   foreach ($tmparr as $v) { 
       $newarr[$i] = $v; 
       $i++; 
 } 
 
// now you can display the results returned. But first we will display the search form on the top of the page
?>
 
<form action="searchresults.php" method="get" name="search">
  <div align="center">
      <input name="q" type="text" value="<?php echo $q; ?>" size="15"> 
      <input name="search" type="submit" value="Search">
  </div>
</form>
 
<?php
// display what the person searched for.
 if( isset ($resultmsg)){
  echo $resultmsg;
  exit();
 }else{
  echo "Search results for: " . $var;
 }
 
foreach($newarr as $value){
 
// EDIT HERE and specify your table and field names for the SQL query
//$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
 $num_value=mysql_query ($query_value);
 $row_linkcat= mysql_fetch_array ($num_value);
 $row_num_links= mysql_num_rows ($num_value);
 
//now let's make the keywods bold. To do that we will use preg_replace function. 
//EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]
//This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line. 
  $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertTitle' ] );
  $deschigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertDescription' ] );
  $service1high = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertService1' ] );
 
foreach($trimmed_array as $trimm){
    if($trimm != 'b' ){
//IF you added more fields to search make sure to add them below as well.
        $titlehigh = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $titlehigh);
        $deschigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $deschigh);
        $service1high = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $service1high); 
     }
//end highlight
 
?>
<br> <br>
<font color="#FF0000">
 
 Advert Id:
<?php echo $row_linkcat['AdvertId'];?></font>   <br>
<?php echo $titlehigh; ?><br>
<?php echo $deschigh; ?><br>
<?php echo $service1high; ?>
 
 
<?php 
}  }  //end foreach $trimmed_array 
   if($row_num_links_main > $limit){
   // next we need to do the links to other search result pages
      if ($s>=1) { // do not display previous link if 's' is '0'
        $prevs=($s-$limit);
         echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
      }
     // check to see if last page
     $slimit =$s+$limit;
       if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
     // not last page so display next link
          $n=$s+$limit;
           echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
       
    }
}  //end foreach $newarr
?>
<br> <br>
Num Rows: <?php echo $row_num_links; ?> <br>

Open in new window

0
 
LVL 17

Expert Comment

by:houssam_ballout
ID: 20341204
Try the Distinct keyword for the ID
instead of using * ,
use select distinct ID, .........
SELECT DISTINCT column_name(s)
FROM table_name
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:long405
ID: 20341224
I did try that one before but that brings back the same results
<?php
// display what the person searched for.
 if( isset ($resultmsg)){
  echo $resultmsg;
  exit();
 }else{
  echo "Search results for: " . $var;
 }
 
foreach($newarr as $value){
 
// EDIT HERE and specify your table and field names for the SQL query
//$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
$query_value = "SELECT DISTINCT AdvertId, AdvertTitle, AdvertDescription FROM advert WHERE AdvertId= '$value'";
 $num_value=mysql_query ($query_value);
 $row_linkcat= mysql_fetch_array ($num_value);
 $row_num_links= mysql_num_rows ($num_value);

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 20342156
The duplicate records are going to occur because of the first loop when you loop through all the search keywords and perform a query for each.  What about  building an sql query based on all the keywords and then executing it once:]

ie:


// try with and without the DISTINCT keyword
$query = "SELECT DISTINCT * FROM advert WHERE ";
foreach ($trimmed_array as $trimm) {
    $query .= "AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ;
}
 
// now perform your query

Open in new window

0
 

Author Comment

by:long405
ID: 20342911
I've trimmed the code right back so that it will just run the one query on the search terms and then display the results as suggested above.
Problem is that if I enter Plumber into the search box, it porduces an :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\appserv\www\searchresults.php on line 30
error.
If I type in Plumber Bathroom into the search box, it brings back

Unable to perform query
SELECT DISTINCT * FROM advert WHERE AdvertTitle LIKE "%plumber%" OR AdvertDescription LIKE "%plumber%" OR AdvertService1 LIKE "%plumber%" AdvertTitle LIKE "%bathroom%" OR AdvertDescription LIKE "%bathroom%" OR AdvertService1 LIKE "%bathroom%"
Seems like it finds two search terms - slits them out and tries to add them to the query however in between the first search term and the second there is no "OR" so it fails.
<?
require "connect.php";
//specify how many results to display per page
$limit = 2;
 
 
// Get the search variable from URL
  $var = @$_GET['q'] ;
//trim whitespace from the stored variable
  $trimmed = trim($var);  
//separate key-phrases into keywords
  $trimmed_array = explode(" ",$trimmed); 
 
// check for an empty string and display a message.
if ($trimmed == "") {
  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
  }
 
// check for a search parameter
if (!isset($var)){
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
  }
  //added bit below
  $query = "SELECT DISTINCT * FROM advert WHERE ";
foreach ($trimmed_array as $trimm) {
    $query .= "AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ;
}
$result =@mysql_query($query,$connection)	
or die("Unable to perform query<br>$query");
while($row = mysql_fetch_array( $query )) 
{?>
<?=$row['AdvertId']?>
<?=$row['AdvertTitle']?>
<?=$row['AdvertDescription']?>
<?=$row['AdvertService1']?>
<?php }
/*
//added bit above
// Build SQL Query for each keyword entered 
//foreach ($trimmed_array as $trimm){
      
// EDIT HERE and specify your table and field names for the SQL query
    // $query = "SELECT * FROM advert WHERE AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ; 
     // Execute the query to  get number of rows that contain search kewords
     $numresults=mysql_query ($query);
     $row_num_links_main =mysql_num_rows ($numresults);
 
     // next determine if 's' has been passed to script, if not use 0.
     // 's' is a variable that gets set as we navigate the search result pages.
     if (empty($s)) {
         $s=0;
     }
 
      // now let's get results.
      $query .= " LIMIT $s,$limit" ;
      $numresults = mysql_query ($query) or die ( "Couldn't execute query" );
      $row= mysql_fetch_array ($numresults);
 
      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
      do{
 //EDIT HERE and specify your field name that is primary key
          $adid_array[] = $row['AdvertId'];
      }while( $row= mysql_fetch_array($numresults));
 //} //end foreach
 
if($row_num_links_main == 0 && $row_set_num == 0){
   $resultmsg = "<p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ;
}
   //delete duplicate record id's from the array. To do this we will use array_unique function
      $tmparr = array_unique($adid_array); 
   $i=0; 
   foreach ($tmparr as $v) { 
       $newarr[$i] = $v; 
       $i++; 
 } 
 
// now you can display the results returned. But first we will display the search form on the top of the page
?>
 
<form action="searchresults.php" method="get" name="search">
  <div align="center">
      <input name="q" type="text" value="<?php echo $q; ?>" size="15"> 
      <input name="search" type="submit" value="Search">
  </div>
</form>
 
<?php
// display what the person searched for.
 if( isset ($resultmsg)){
  echo $resultmsg;
  exit();
 }else{
  echo "Search results for: " . $var;
 }
 
foreach($newarr as $value){
   
// EDIT HERE and specify your table and field names for the SQL query
//$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
$query_value = "SELECT DISTINCT AdvertId, AdvertTitle, AdvertDescription, AdvertService1 FROM advert WHERE AdvertId= '$value'";
 $num_value=mysql_query ($query_value);
 $row_linkcat= mysql_fetch_array ($num_value);
 $row_num_links= mysql_num_rows ($num_value);
 
//now let's make the keywods bold. To do that we will use preg_replace function. 
//EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]
//This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line. 
  $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertTitle' ] );
  $deschigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertDescription' ] );
  $service1high = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertService1' ] );
 
foreach($trimmed_array as $trimm){
    if($trimm != 'b' ){
//IF you added more fields to search make sure to add them below as well.
        $titlehigh = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $titlehigh);
        $deschigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $deschigh);
        $service1high = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $service1high); 
     }
//end highlight
 
?>
<br> <br>
<font color="#FF0000">
 
 Advert Id:
<?php echo $row_linkcat['AdvertId'];?></font>   <br>
<?php echo $titlehigh; ?><br>
<?php echo $deschigh; ?><br>
<?php echo $service1high; ?>
 
 
<?php 
}  
}  //end foreach $trimmed_array 
   if($row_num_links_main > $limit){
   // next we need to do the links to other search result pages
      if ($s>=1) { // do not display previous link if 's' is '0'
        $prevs=($s-$limit);
         echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
      }
     // check to see if last page
     $slimit =$s+$limit;
       if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
     // not last page so display next link
          $n=$s+$limit;
           echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
       
    }
}  //end foreach $newarr
?>
<br> <br>
Num Rows: <?php echo $row_num_links; ?> <br>
*/

Open in new window

0
 
LVL 21

Accepted Solution

by:
nizsmo earned 75 total points
ID: 20344096
This should work and generate the queries correctly.
<?
require "connect.php";
//specify how many results to display per page
$limit = 2;
 
 
// Get the search variable from URL
  $var = @$_GET['q'] ;
//trim whitespace from the stored variable
  $trimmed = trim($var);  
//separate key-phrases into keywords
  $trimmed_array = explode(" ",$trimmed); 
 
// check for an empty string and display a message.
if ($trimmed == "") {
  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
  }
 
// check for a search parameter
if (!isset($var)){
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
  }
  //added bit below
  $query = "SELECT DISTINCT * FROM advert WHERE ";
  
  $count = 1;
foreach ($trimmed_array as $trimm) {
	if($count==1)
	{
    	$query .= "AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ;
    	$count++;
    	continue;
	}
    $query .= "OR AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ;
}
$result =@mysql_query($query,$connection)	
or die("Unable to perform query<br>$query");
while($row = mysql_fetch_array( $query )) 
{?>
<?=$row['AdvertId']?>
<?=$row['AdvertTitle']?>
<?=$row['AdvertDescription']?>
<?=$row['AdvertService1']?>
<?php }
/*
//added bit above
// Build SQL Query for each keyword entered 
//foreach ($trimmed_array as $trimm){
      
// EDIT HERE and specify your table and field names for the SQL query
    // $query = "SELECT * FROM advert WHERE AdvertTitle LIKE \"%$trimm%\" OR AdvertDescription LIKE  \"%$trimm%\" OR AdvertService1 LIKE \"%$trimm%\" " ; 
     // Execute the query to  get number of rows that contain search kewords
     $numresults=mysql_query ($query);
     $row_num_links_main =mysql_num_rows ($numresults);
 
     // next determine if 's' has been passed to script, if not use 0.
     // 's' is a variable that gets set as we navigate the search result pages.
     if (empty($s)) {
         $s=0;
     }
 
      // now let's get results.
      $query .= " LIMIT $s,$limit" ;
      $numresults = mysql_query ($query) or die ( "Couldn't execute query" );
      $row= mysql_fetch_array ($numresults);
 
      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
      do{
 //EDIT HERE and specify your field name that is primary key
          $adid_array[] = $row['AdvertId'];
      }while( $row= mysql_fetch_array($numresults));
 //} //end foreach
 
if($row_num_links_main == 0 && $row_set_num == 0){
   $resultmsg = "<p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ;
}
   //delete duplicate record id's from the array. To do this we will use array_unique function
      $tmparr = array_unique($adid_array); 
   $i=0; 
   foreach ($tmparr as $v) { 
       $newarr[$i] = $v; 
       $i++; 
 } 
 
// now you can display the results returned. But first we will display the search form on the top of the page
?>
 
<form action="searchresults.php" method="get" name="search">
  <div align="center">
      <input name="q" type="text" value="<?php echo $q; ?>" size="15"> 
      <input name="search" type="submit" value="Search">
  </div>
</form>
 
<?php
// display what the person searched for.
 if( isset ($resultmsg)){
  echo $resultmsg;
  exit();
 }else{
  echo "Search results for: " . $var;
 }
 
foreach($newarr as $value){
   
// EDIT HERE and specify your table and field names for the SQL query
//$query_value = "SELECT * FROM advert WHERE AdvertId= '$value'";
$query_value = "SELECT DISTINCT AdvertId, AdvertTitle, AdvertDescription, AdvertService1 FROM advert WHERE AdvertId= '$value'";
 $num_value=mysql_query ($query_value);
 $row_linkcat= mysql_fetch_array ($num_value);
 $row_num_links= mysql_num_rows ($num_value);
 
//now let's make the keywods bold. To do that we will use preg_replace function. 
//EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ]
//This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line. 
  $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertTitle' ] );
  $deschigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertDescription' ] );
  $service1high = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'AdvertService1' ] );
 
foreach($trimmed_array as $trimm){
    if($trimm != 'b' ){
//IF you added more fields to search make sure to add them below as well.
        $titlehigh = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $titlehigh);
        $deschigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $deschigh);
        $service1high = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" , $service1high); 
     }
//end highlight
 
?>
<br> <br>
<font color="#FF0000">
 
 Advert Id:
<?php echo $row_linkcat['AdvertId'];?></font>   <br>
<?php echo $titlehigh; ?><br>
<?php echo $deschigh; ?><br>
<?php echo $service1high; ?>
 
 
<?php 
}  
}  //end foreach $trimmed_array 
   if($row_num_links_main > $limit){
   // next we need to do the links to other search result pages
      if ($s>=1) { // do not display previous link if 's' is '0'
        $prevs=($s-$limit);
         echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
      }
     // check to see if last page
     $slimit =$s+$limit;
       if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
     // not last page so display next link
          $n=$s+$limit;
           echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
       
    }
}  //end foreach $newarr
?>
<br> <br>
Num Rows: <?php echo $row_num_links; ?> <br>
*/

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
remote mysql 8 39
PHP function parameters defined 14 41
htaccess block all countries but the US 4 39
Problem with MySQL query - graph 3 26
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question