creating a search function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nineseveninteractive
Forum Newbie
Posts: 7
Joined: Mon Jun 25, 2007 6:13 pm

creating a search function

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

This would involve AJAX or Javascript.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Beyond what it would look like, you'll need to be using a database. That's where you should start your research.
nineseveninteractive
Forum Newbie
Posts: 7
Joined: Mon Jun 25, 2007 6:13 pm

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
undecided name 01
Forum Newbie
Posts: 12
Joined: Mon Jul 02, 2007 9:25 am
Contact:

Post 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);
}
Last edited by undecided name 01 on Tue Jul 03, 2007 8:25 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The above post by WebWave.ir is dangerous to use without further modification.
User avatar
undecided name 01
Forum Newbie
Posts: 12
Joined: Mon Jul 02, 2007 9:25 am
Contact:

Post by undecided name 01 »

Yes. You may want to validate and refine $_GET[*] variables before inserting them into the SQL-query.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
undecided name 01
Forum Newbie
Posts: 12
Joined: Mon Jul 02, 2007 9:25 am
Contact:

Post 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);
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_key_exists() offers slightly differing functionality from isset() however.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:array_key_exists() offers slightly differing functionality from isset() however.
Ooh. That's actually useful.
User avatar
undecided name 01
Forum Newbie
Posts: 12
Joined: Mon Jul 02, 2007 9:25 am
Contact:

Post 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).
Post Reply