Solved

PHP/MYSQL/JAVA Search Box

Posted on 2012-03-29
1
320 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
[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
1 Comment
 
LVL 83

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

756 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