Jump to content


Photo

Search term help...


  • Please log in to reply
3 replies to this topic

#1 oMIKEo

oMIKEo
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationLeeds, UK

Posted 08 July 2006 - 09:42 PM

Hi,

Ive created a search area to a site but its not working too good...

The scripts checks a 'thetype' field and then checks a 'keywords' field.

The 'thetype' can only be either Perminent or Contract (or Any)
The 'keywords' can be anything and are stored in the database seporated by commas (e.g. key1, key2, key3)

It wont let you seach for multiple keywords at once, how can i improve this script to its not a picky but still relevant when finding results?

<?php
	
	$conn=mysql_connect("$db_host","$db_username","$db_password") or die("Err:Conn");
	$rs = mysql_select_db("$db_main",$conn) or die("Err:Db");
	
	$thetype = $_POST['types'];	
	$keywords = $_POST['keywords'];
	
	if($thetype == "Any")
	{
		if($keywords == "");
		$sql="select * from rec_jobs";

		if($keywords != "");
		$sql="select * from rec_jobs where keywords LIKE '%$keywords%'";
	}
	
	if($thetype != "Any")
	{
		if($keywords == "");
		$sql="select * from rec_jobs where `type` = '$thetype'";

		if($keywords != "");
		$sql="select * from rec_jobs where `type` = '$thetype' AND keywords LIKE '%$keywords%'";
	}

	$rs=mysql_query($sql,$conn) or die("Could not execute query: ".mysql_error());
    $thenum = mysql_num_rows($rs);
	
	if ($thenum != 0)
	echo '<table style="width:700px;">
	<tr>
				<td width="33%" height="32px"><strong>Job Title</strong></td>
				<td width="33%" height="32px"><strong>Location</strong></td>
				<td width="33%" height="32px"><strong>Salary</strong></td>
	
	 </tr>';
	 else
	 echo '<table style="width:100%">
	<tr>
				<td></td>
	
	 </tr>';
	
	while($row=mysql_fetch_array($rs))
	{
			$job_id = ''.$row[id].'';
			$title = ''.$row[title].'';
			$description = ''.$row[description].'';
			$type = ''.$row[type].'';
			$location = ''.$row[location].'';
			$salary = ''.$row[salary].'';
			$featured = ''.$row[featured].'';
		
			echo '<tr>
					<td width="33%" height="32px"><a href=job_details.php?id='.$job_id.'>'.$title.'</a></td>
					<td width="33%" height="32px">'.$location.'</td>
					<td width="33%" height="32px">'.$salary.'</td>
				  </tr>';
			
	}
	
	echo '</table>';
	
	
	if ($thenum == 0)
		echo '<strong>No jobs found</strong><br><br> <a href="job_seekers.php">Try Again</a>';
		else
		echo '<br><br><a href="job_seekers.php"><strong>New Search</strong></a>';
?>

Thanks,
Mike

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 09 July 2006 - 03:34 AM

Need to do something like this:

$words = explode(' ', $keywords);
$search = "keywords LIKE '%" . implode("%' OR keywords LIKE '%", $words) . "%'";

Then replace this:
$sql="select * from rec_jobs where keywords LIKE '%$keywords%'";

with this:
$sql="select * from rec_jobs where $search";

And replace this:
$sql="select * from rec_jobs where `type` = '$thetype' AND keywords LIKE '%$keywords%'";

with this:
$sql="select * from rec_jobs where `type` = '$thetype' AND ($search)";


Please note that you have semi-colons on the "if" line which is incorrect. You can also use "else" to help. Example:

<?PHP
...
$thetype = isset($_POST['types']) ? $_POST['types'] : 'Any';
$keywords = isset($_POST['keywords']) ? $_POST['keywords'] : '';

$words = explode(' ', $keywords);
$search = "keywords LIKE '%" . implode("%' OR keywords LIKE '%", $words) . "%'";

if ('Any' == $thetype)
{
    if (empty($keywords))
      $sql="select * from rec_jobs";
    else
      $sql="select * from rec_jobs where $search";

} else {

    if (empty($keywords))
      $sql="select * from rec_jobs where `type` = '$thetype'";
    else
      $sql="select * from rec_jobs where `type` = '$thetype' AND ($search)";

}
...
?>



#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 09 July 2006 - 06:45 AM

You may also consider fulltext searches.

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 09 July 2006 - 03:47 PM

Yes, full-text searches give you search capabilities similar to google searches. For better performance you will need to have a FULLTEXT index created.

It does have some limitations unless you have full access to MySQL environment. For instance by default it will ignore words of four characters or less. This maybe ok for you, but you can't change it unless you set a system variable (ft_min_word_len) in MySQL version 4+. If you change this system variable value after you have data in the table, then you'll have to reindex your full-text column(s).

Here's a list of words that MySQL will ignore (not index by default):
http://dev.mysql.com...-stopwords.html





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users