Page 1 of 1

creating a search function

Posted: Thu Jun 28, 2007 2:26 pm
by nineseveninteractive
Hi

I need to create a search function for a recruitment company website that allows the user to select from one drop down box whether the job is permanent, temporary or search all jobs and from that select the job category.

i have tried looking for tutorials on the web but cant find anything. Would anyone be able to point me in the right direction to find a tutorial to help me complete this?

it needs to work like the advanced search function on this webiste but only have the Job Type and Job Category search options:

http://www.spring.com/springpersonnel/c ... Search.asp

many thanks

Posted: Thu Jun 28, 2007 6:15 pm
by s.dot
This would involve AJAX or Javascript.

Posted: Thu Jun 28, 2007 7:04 pm
by superdezign
Beyond what it would look like, you'll need to be using a database. That's where you should start your research.

Posted: Sun Jul 01, 2007 5:33 am
by nineseveninteractive
Hi

Thank you for the replies.

I have the database set up and the site is working, the only thing i have left to do on it is create this search feature.

What is the correct term for this type of search? I am searching the web for "php drop down list search" and various other similar search terms but can't find anything

many thanks :D

Posted: Sun Jul 01, 2007 6:09 am
by superdezign
nineseveninteractive wrote:What is the correct term for this type of search? I am searching the web for "php drop down list search" and various other similar search terms but can't find anything
Wha? This shouldn't be too hard. What you want to do is use the drop down to select a category. Then, you post that category and display all database entries that are marked with that category.

Posted: Tue Jul 03, 2007 8:21 pm
by undecided name 01
In case you're asking how to implement such a search:

Code: Select all

$db_result = mysql_query("
  SELECT * FROM jobs
  WHERE
      job_type = '" . $_GET['job_type'] . "' AND
      job_category =  '" . $_GET['job_category'] . "'
");

while ($db = mysql_fetch_assoc($db_result)) {
   print_r($db);
}

Posted: Tue Jul 03, 2007 8:23 pm
by feyd
The above post by WebWave.ir is dangerous to use without further modification.

Posted: Tue Jul 03, 2007 8:31 pm
by undecided name 01
Yes. You may want to validate and refine $_GET[*] variables before inserting them into the SQL-query.

Posted: Tue Jul 03, 2007 8:35 pm
by superdezign
WebWave.ir wrote:Yes. You may want to validate and refine $_GET[*] variables before inserting them into the SQL-query.
Change "may" to "will," and add "escape" in with "validate and refine," and you'll be correct.

Posted: Tue Jul 03, 2007 8:51 pm
by undecided name 01
Thanks for the notes.
Assuming you're using Integer values for job_type and job_category in your
database, this is a solution:

Code: Select all

$job_type = array_key_exists('job_type', $_GET) ? intval($_GET['job_type']) : 0;
$job_category = array_key_exists('job_category', $_GET) ? intval($_GET['job_category']) : 0;

if ($job_type == 0) {
	die("Invalid Job Type.");
}

if ($job_category == 0) {
	die("Invalid Job Category.");
}

$db_result = mysql_query("
  SELECT * FROM jobs
  WHERE
      job_type = '" . mysql_real_escape_string($job_type) . "' AND
      job_category =  '" . mysql_real_escape_string($job_category) . "'
") or die("Unable to perform search.");

while ($db = mysql_fetch_assoc($db_result)) {
   print_r($db);
}

Posted: Tue Jul 03, 2007 9:27 pm
by superdezign
A lot of array functions are pretty old. Instead of array_key_exists(), you can just use isset($_GET['index']). And for the invalid job type, you should actually use NULL instead of 0, since 0 is a valid integer.

Posted: Tue Jul 03, 2007 9:29 pm
by feyd
array_key_exists() offers slightly differing functionality from isset() however.

Posted: Tue Jul 03, 2007 9:37 pm
by superdezign
feyd wrote:array_key_exists() offers slightly differing functionality from isset() however.
Ooh. That's actually useful.

Posted: Wed Jul 04, 2007 5:31 am
by undecided name 01
A lot of array functions are pretty old.
Because array_key_exists() is older than isset() you prefer to use isset()?
Of course these functions are different. But I think that they act similarly in this type of Form Handling.
Would you please offer a sample script to demonstrate the difference in a Form Handling situation? and a better reason to use isset()? What's wrong with array_key_exists()?
And for the invalid job type, you should actually use NULL instead of 0, since 0 is a valid integer.
There are three situations where job_type is invalid:
  • 1. It is not available;
    2. It is not an Integer;
    3. It is not in the valid domain (1..MAX(job_type));
Using 0 to express an invalid value is better than NULL here, since intval("Some String") returns 0 instead of NULL. Of course, 0 is a valid integer, but do you use 0 to express a job_type?

I even rather to use "if ($job_type < 1)" to check for invalid values, since we do not use zero and negative values for primary keys in the database (job_type is a Foreign Key which references to job_type in the table job_types, job_type is primary key in the table job_types).