0

Hello all,

I've ran into a bit of a problem and I can't figure out how to get out of it.

Basically, I have an autosuggest input field on my website and I would like to get data from 2 mysql tables.

It is similar to yell.com where you search for your "keyword" or "company name" and it all gets returned.

www.whatslocaltome.co.uk is the website. You can see my autosuggest box at the top (Keyword)... I want it to return either towns or company names...

Code so far:

$sql_res=mysql_query("select * from directory_categories where `category` like '%$q%' order by id LIMIT 5");
if($sql_res > 0){
	while($row=mysql_fetch_array($sql_res))
	{
	$kw=$row['category'];
?>
		<div class="display_box" align="left">
		<a href="" onClick="document.forms['searchform'].keyword.value = '<?php echo $kw; ?>'; return false;" id="resultsclose" onclick="fill();" >
<?php 
		echo $kw; 
?>
		</a>
		</div>
<?php
	}
}
elseif(!$sql_res) {
?>
	<div class="display_box" align="left">
<?php
	echo "No suggestion";?>
		</div>
<?php
	}
}
else
{
}
?>
2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by cwarn23
-1

I would send you a video about jQuery but I'm not allowed according to happygeek so I can't answer that question for now.

Votes + Comments
yawn
0

I don't have a problem with jQuery. It's the PHP coding I am struggling with a bit. I need to know how to retrieve the data from both tables on the fly and display them in 1 output.

0

Don't you need an ajax page for the autosuggest to do a query on your database, fetch and put onto the page.

0

I've got the ajax sorted for it. It is all in place and works as it is at the moment. But at the moment it's only setup to query the categories table. I want to be able to query the categories AND towns table at the same time. No data is the same in either tables.

0

The code at the top of the topic is the php file that is called via ajax.

This is the ajax code that calls it:

$(document).ready(function(){
$(".search").keyup(function() 
{
var searchbox = $(this).val();
var dataString = 'keyword='+ searchbox;
if(searchbox=='')
{}
else
{
	$.ajax({
	type: "POST",
	url: "results.php",
	data: dataString,
	cache: false,
	success: function(html)
{
$('#display').fadeIn();
}
});
}return false; 
});
0

jQuery again :(
I don't know much about jQuery but know enough to say that jQuery isn't a good tool to use particularly for styling and functions. I dunno about you but I find ajax a mind bobling experience and a lot simpler and more efficient to deal with raw Javascript. As per how to do this job, the question isn't exactly clear although I do get that you want to select two tables at the same time. You haven't said what you need to do with them I'm kinda guessing here. My guess would by you want like a Join on the query. The best way to do a join in that case would be to have a third table which has 2 columns where column one is in table one and column two is in table two. This way you can do a 3 way join. If however you want to retrieve the data with one ajax request then you will probably want a separator of some sort and get javascript to do the php equivalent of explode(). I'm about to go to bed but I'll be back online tomorrow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.