Solved

PHP/MYSQL/JAVA Search Box

Posted on 2012-03-29
1
317 Views
Last Modified: 2012-08-14
Hi, i have the below code working for my dynamic website search.
This works fine, however I'm having trouble implementing a message saying "No Result" when a user types a string that doesn't match anything in the database, like for example, the user types in the letter 'z' which doesn't match any titles.

Also, how would i implement a cache so the input doesn't keep calling the database every time a letter is pressed? Still trying to figure all this java stuff out...could someone please help me with this?!

function getSuggestions(value) {
	if(value != "") {
		$.post("/search.php", {userPart:value}, function(data) {
			$("#search_display").html(data);
		});
	} else {
		removeSuggestions();
	}
}
function removeSuggestions() {
	$("#search_display").html("");
}
function addText(value) {
	$("#search-box").val(value);
}

Open in new window


$userPart = mysql_real_escape_string(addslashes($_POST['userPart']));
$search_result = mysql_query("SELECT... WHERE subcat_name LIKE '%".$userPart."%' LIMIT 0, 16") or die (mysql_error());

while ($row_search_result = mysql_fetch_assoc($search_result)) {
	echo "<div class='link' onClick='addText(\"".$row_search_result['subcat_name']."\");'><a href='".$row_search_result['search_url']."'>".$row_search_result['subcat_name']."</a><br />in ".$row_search_result['cat_name']."</div>";
}

Open in new window

0
Comment
Question by:JiveMedia
1 Comment
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37785474
Check the number of rows returned.

$userPart = mysql_real_escape_string(addslashes($_POST['userPart']));
$search_result = mysql_query("SELECT... WHERE subcat_name LIKE '%".$userPart."%' LIMIT 0, 16") or die (mysql_error());
$num_rows = mysql_num_rows($search_result);
if($num_rows == 0) echo "<div>No Results.</div>";
else {
while ($row_search_result = mysql_fetch_assoc($search_result)) {
	echo "<div class='link' onClick='addText(\"".$row_search_result['subcat_name']."\");'><a href='".$row_search_result['search_url']."'>".$row_search_result['subcat_name']."</a><br />in ".$row_search_result['cat_name']."</div>";
}
}

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to count occurrences of each item in an array.

747 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

10 Experts available now in Live!

Get 1:1 Help Now