Solved

PHP/MYSQL/JAVA Search Box

Posted on 2012-03-29
1
326 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.

628 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