Forum Moderators: coopster
As you know, I am still trying to learn PHP.
What I am trying to do is create a search script for my Job Posting Site.
The code that was being used didnt work as whe someone searched for example (chef), it returned results. But if they searched for (chef anyword) it didnt return any results.
So, I am trying from scratch to create a new script
I am REALLY stuck trying to figure out this code :(
Can anyone please help?
Code on job search form:
<form action=job-search2.php method=get><input type='text' name='keyword'>
<td>Type of employment:</td>
<td>
<select name="employment_type">
<OPTION VALUE=""></OPTION>
<OPTION VALUE="Not Stated">Not Stated</OPTION>
<OPTION VALUE="Contract">Contract</OPTION>
<OPTION VALUE="Temp">Temp</OPTION>
<OPTION VALUE="Casual">Casual</OPTION>
<OPTION VALUE="Part Time">Part Time</OPTION>
<OPTION VALUE="Full Time">Full Time</OPTION>
</select>
</td>
I can see in theory how this has to work but I dont have the confidence or knowlege to work it out.
select from job_post where position LIKE '%$_GET[kw]%' description LIKE '%$_GET[kw]%'
* I have read somewhere that the query needs to be in an array to get the 'search for any of the keywords' trick to work, is that right?
* If the user selects or doesnt select an $employment type it will have to take that into account.
<form action="job-search2.php" method="post">
Name of the search field is keyword, not kw
<input type="text" name="keyword">
So to get that input data use $_POST['keyword'].
position is <select name="employment_type">
So to get that info you check the $_POST['employment_type']
The query is logic based so WHERE a=1 AND b=1 OR a=2 (you need the AND and OR)
Moreover you need to select something, * means everything, however you can also SELECT job, description, position, etc...
This will be your query:
$sql = "SELECT * FROM job_post WHERE
position = '".mysql_real_escape_string($_POST['employment_type'])."' AND description LIKE '%".mysql_real_escape_string($_POST['keyword']."%'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['position']."<br>".$row['description']."<br><br>";
}
Hope this helps!
Michal Cibor
I tried what you suggested and got the following error:
Parse error: parse error, unexpected ';' in /home/httpd/vhosts/example.com/httpdocs/jobseekers/search-form2.php on line 10
$sql = "SELECT * FROM job_post WHERE position = '".mysql_real_escape_string($_POST['employment_type'])."' AND description LIKE '%".mysql_real_escape_string($_POST['keyword']."%'";// return results
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['position']."<br>".$row['description']."<br><br><br><br>";
}
?>
[edited by: jatar_k at 4:09 pm (utc) on Sep. 21, 2005]
[edit reason] examplified [/edit]