Jump to content

Full Text highlighted variable


dennismonsewicz

Recommended Posts

I am implementing a search script into a database intranet and I was wondering if someone could help me figure out how to highlight the inputed search string?

 

Here is my code so far:

 

 $var = @$_POST['q'];			  
$trimmed = trim($var); 

 

That isn't all of my code, but the other code probably is relevant to my question. So anyone got any ideas?

Link to comment
Share on other sites

Your solution works to just echo out the entered values into the fields, but you can't do a SQL search with the str_replace code. This is what I have so far:

 

 $search1 = @$_POST['search1'];
		  $search2 = @$_POST['search2'];
		  
		  $trimmed1 = trim($search1); //trim whitespace from the stored variables
		  $trimmed2 = trim($search2);
		  
		  $newtrimmed1 = str_replace($trimmed, "<span class='yellowhighlight'>$search1</span>", $trimmed1);
		  $newtrimmed2 = str_replace($search2, "<span class='yellowhighlight'>$search2</span>", $trimmed2);
		  
		  
			/*
			// PHP Code that is used to write to a text file. Is not working right now, so is down at the moment
			$myFile = "results.txt";
			$fh = fopen($myFile, 'a') or die("can't open file");
			$stringData = $trimmed;
			fwrite($fh, $stringData); 
			*/

		// rows to return
		$limit=9999; 

		// check for an empty string and display a message.
		if ($trimmed1 && $trimmed2 == "")
		  {
		  echo "<p class='contenttext'>Please enter a search...</p>";
		  include "../includes/footer.php";
		  exit;
		  }

		// check for a search parameter
		if (!isset($search1))
		  {
		  echo "<p class='contenttext'>We dont seem to have a search parameter!</p>";
		  include "../includes/footer.php";
		  exit;
		  } else if(!isset($search2))
		  	{
				echo "<p class='contenttext'>We don't seem to have a search parameter! Both Fields must be filled out!</p>";
				include "../includes/footer.php";
				exit;
				}

		//connect to your database ** EDIT REQUIRED HERE **
		mysql_connect("localhost","DB_username","DB_password"); //(host, username, password)

		//specify database ** EDIT REQUIRED HERE **
		mysql_select_db("DB_NAME") or die("Unable to select database"); //select which database we're using

		// Build SQL Query  
		$query = "(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from arcamax where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from google where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from drudge where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from human_events where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from newsmax where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from street where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from townhall where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from weatherbug where start_end_date in ('$trimmed1',  '$trimmed2')) union 
(select id,promo_code,start_end_date,mailedlist,description,orders,totalrevenue from worldnet where start_end_date in ('$trimmed1',  '$trimmed2')) order by mailedlist";

		 $numresults=mysql_query($query)or die(mysql_error()." With query $query");

		 $numrows=mysql_num_rows($numresults);

		// If we have no results, offer a google search as an alternative

		if ($numrows == 0)
		  {
		  echo "<h4 class='contenttext'>Results</h4>";
		  echo "<p class='contenttext'>Sorry, your search: <span class='yellowhighlight'> <b>" . $trimmed1 . "</b> & <b>" . $trimmed2 . "</b> </span> returned zero results</p>";

		/*// google
		 echo "<p><a href=\"http://www.google.com/search?q=" 
		  . $trimmed . "\" target=\"_blank\" title=\"Look up 
		  " . $trimmed . " on Google\">Click here</a> to try the 
		  search on google</p>";*/
		  include "../includes/footer.php";
		  exit;
		  }

		// next determine if s has been passed to script, if not use 0
		  if (empty($s)) {
		  $s=0;
		  }

		// get results
		  $query .= " limit $s,$limit";
		  $result = mysql_query($query) or die("Couldn't execute query");

		// display what the person searched for
		echo "<p class='contenttext'>You searched for: <b>" . $newtrimmed1 . "</b> & <b>" . $newtrimmed2 . "</b></p>";

		// begin to show results set
		echo "<p class='contenttext'>Results (Click a product below to view more information) <br /><br />";
		$count = 1 + $s ;

		// now you can display the results returned
		 echo '<div class="tabledb">
						<table class="table" border="0" align="center" cellpadding="4" cellspacing="0" width="900">
							<tr>
								<td class="headline" width="25"> </td>
								<td class="headline" width="100">Promo Code</td>
								<td class="headline" width="75">Start/End Date</td>
								<td class="headline" width="250">Vendor</td>
								<td class="headline" width="300">Description</td>
								<td class="headline" width="150">Total Revenue</td>
								<td class="headline" width="100">View More?</td>
							</tr>';
                
                while($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					$id = $row['id'];
					$promo_code = $row['promo_code'];
					$start_end_date = $row['start_end_date'];
					$vendor = $row['mailedlist'];
					$description = $row['description'];
					$totalrevenue = $row['totalrevenue'];

						echo "<tr>";
						echo "<td class='result'>$count</td>";
						echo "<td class='result'>$promo_code</td>";
						echo "<td class='result'>$start_end_date</td>";
						echo "<td class='result'>$vendor</td>";
						echo "<td class='result'>$description</td>";
						echo "<td class='result'>$totalrevenue</td>";
						echo "<td class='result' align='center'><a href=\"mediacase.php?action=view&id=" . $id . "&db=" . $db . "\">View More…</a></td>";
						echo "</tr>";
						$count++;

				}
						echo "</table>";
?>

 

The SQL statement uses the $trimmed1 and $trimmed2 values to search the databases. How do I still highlight the users search throughout the results?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.