Solved

PHP/MYSQL/JAVA Search Box

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 49
Angular - break the dataset in chunks? 7 39
html input type 3 21
Convert html page to a php post email form automatically? 10 27
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 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

21 Experts available now in Live!

Get 1:1 Help Now