Jump to content

a couple of enhancements I would like to make to my search form


webguync

Recommended Posts

Hi, I have a search form which pulls info from a MySQL table and there are a few enhancements I would like to make.

1) I would like the search terms in my results table hilited in red. I made a class in my stylesheet for this, but it isn't working so I am missing a step or two

2) I would like a display for the number of results returned. Example. "There were 3 results found in your search".

3) This isn't php related, but if anyone has any ideas why my JQuery slideup doesn't work with my results let me know. I have posted in the JavaScript section, but haven't gotten a response.

 

My code.

 

<html>
<head>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
<script src="js/jquery.js"></script>
<script src="js/jquery-fonteffect-1.0.0.js"></script>
<script type="text/javascript">
$("#mirror").FontEffect({ outline:true })
</script>
<script type='text/javascript'>
var $ = jQuery.noConflict();

$(document).ready(function(){

$("#search_results").slideUp(); 
    $("#search_button").click(function(e){
        e.preventDefault();
        ajax_search();

    });
   // $("#search_term").keyup(function(e){
     //   e.preventDefault();
      //  ajax_search();
   // });

});
function ajax_search(){
  $("#search_results").show();
  var search_val=$("#search_term").val();
  $.post("./find.php", {search_term : search_val}, function(data){
   if (data.length>0){
     $("#search_results").html(data);
$(document).ready(function(){
   $(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
   $(".stripeMe tr:even").addClass("alt");
});
   }
  })
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />

<title>Novo RPC Results Search Engine</title>

<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="mirror">Search RPC participants</div>
    <form id="searchform" method="post" action="find.php">
<div>
        <label for="search_term">Search RPC information</label>
        <input type="text" name="search_term" id="search_term" />
<input type="submit" value="search" id="search_button" />
</div>
    </form>
    <div id="search_results"></div>
</body>
</html> 

 

 

<?php
define(HOST, "localhost");
define(USER, "username");
define(PW, "pw");
define(DB, "DBName");

$connect = mysql_connect(HOST,USER,PW)
or die('Could not connect to mysql server.' );
mysql_select_db(DB, $connect)
or die('Could not select database.');
$term = strip_tags(substr($_POST['search_term'],0, 100));
$term = mysql_escape_string($term); 
$sql = "SELECT *
FROM Phase1A_1B_TotalScores_2011
WHERE
CONCAT(last_name,first_name,employee_id,title,territory,district,Phase1A_Score,Phase1B_HS_Exam,
Phase1A_HS_Exam_RT,Phase1B_HS_Exam ,Phase1B_HS_Exam_RT,Class_Date)
LIKE '%$term%'
order by last_name asc";
$string = '';
$string = "<table class='stripeMe' id='Results'><tr><th>Last Name</th><th>First Name</th><th>Employee ID</th><th>Title</th><th>Territory</th><th>District</th><th>Phase 1A Score</th><th>Phase 1B Score</th><th>Phase 1 Average</th><th>Phase 1A HS Exam</th><th>Phase 1A HS Exam Retake</th><th>Phase 1B HS Exam</th><th>Phase 1B HS Exam Retake</th><th>Class Dates</th><th>Awards</th></tr>";

$result = mysql_query($sql);  /// This is the execution
if (mysql_num_rows($result) > 0){
  while($row = mysql_fetch_object($result)){
 $string .= "<tr>";
    $string .= "<td>".$row->last_name."</td> ";
     $string .= "<td>".$row->first_name."</td>";
  $string .= "<td>".$row->employee_id."</td>";
   $string .= "<td>".$row->title."</b>";
      $string .= "<td>".$row->territory."</td>";
	   $string .= "<td>".$row->district."</td>";
	    $string .= "<td>".$row->Phase1A_Score."</td>";
	  		   $string .= "<td>".$row->Phase1B_Score."</td>";
			    $string .= "<td>".$row->Phase1_Average."</td>";
	   $string .= "<td>".$row->Phase1A_HS_Exam."</td>";
	   $string .= "<td>".$row->Phase1A_HS_Exam_RT."</td>";
	    $string .= "<td>".$row->Phase1B_HS_Exam."</td>";
	   $string .= "<td>".$row->Phase1B_HS_Exam_RT."</td>";
	    $string .= "<td>".$row->Class_Date."</td>";
		 $string .= "<td>".$row->Awards."</td>";
    $string .= "<br/>\n";
$string .= "</tr>";

    //print_r($row);
  }
$string .= "</table>";
}else{

  $string = "<span class='NMF'>No matches found!</span>";
//  echo $sql;

} 

echo $string;


?>

 

and lastly my CSS

 

/*search term styling*/
#search_term{
font-weight:bold;
color:#f00;
}

Link to comment
Share on other sites

To be honest, I read your post yesterday and decided not to respond because you just simply posted all of your code and expected us to read it all to understand the logic and tell you how to do it. I'm not willing to invest the time needed to do that. You need to provide the specific information around a particular problem and make it easier for us volunteers to help you.

 

For example, regarding the highlighting problem I really don't understand what you are doing. You specified a style for an ID (e.g. #search_term). There can only be one element on a page with a particular ID. So, if there were multiple instances of the search term I don't know how you expect to apply that style to each of them. I would think you would want to create a named class (i.e. .search_term).

 

Second, you have some JavaScript which seems to be for applying the style? Not sure, but I see the "#search_term" name in the JavaScript. But, since I see nothing in your PHP code for applying the style property I assume you are trying to do it with JS.

 

So, for the style issue, first create the style as a class. Then implement some functionality in the PHP code to apply the style to the seach term in the output. Personally I would probably do something right at the beginning of the while() loop to go through all of the record values and modify them to highlight the search term if it exists.

 

Here is an example (not sure it is the correct syntax for an object

  while($row = mysql_fetch_object($result)){
    foreach($row as $key -> $value)
    {
        $row->$key = str_ireplace($term, "<span class=\"search_term\">{$term}</span>", $value);
    }

    $string .= "<tr>";

 

Note: that is not tested and has a problem with your current code. At the top of your page you apply some parsing to the search term:

$term = strip_tags(substr($_POST['search_term'],0, 100));
$term = mysql_escape_string($term); 

 

You want to use the first $term (before applying mysql_escape_string()) for the str_ireplace() function. So, you should do something like

$term = strip_tags(substr($_POST['search_term'],0, 100));
$termSQL = mysql_escape_string($term); 

 

And then use $termSQL in the DB querry

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.