Jump to content

Search term help...


oMIKEo

Recommended Posts

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?

[code]<?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>';
?>[/code]

Thanks,
Mike
Link to comment
https://forums.phpfreaks.com/topic/14055-search-term-help/
Share on other sites

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:

[code]
<?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)";

}
...
?>
[/code]

Link to comment
https://forums.phpfreaks.com/topic/14055-search-term-help/#findComment-55026
Share on other sites

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/doc/refman/4.1/en/fulltext-stopwords.html
Link to comment
https://forums.phpfreaks.com/topic/14055-search-term-help/#findComment-55198
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.