Solved

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

Posted on 2007-11-23
9
400 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
9 Comments
 
LVL 17

Expert Comment

by:houssam_ballout
Comment Utility
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
Comment Utility
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
Comment Utility
Try the Distinct keyword for the ID
instead of using * ,
use select distinct ID, .........
SELECT DISTINCT column_name(s)
FROM table_name
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:long405
Comment Utility
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 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now