creating the searching in php

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
hmxa
Forum Newbie
Posts: 4
Joined: Tue Jan 04, 2011 1:15 pm

creating the searching in php

Post by hmxa »

hey guys, im new to all this php stuff.
I have a project in this semester to create an HR contract system for the university, here are my requirements:
1. Add Contracts
2. Delete Contracts
3. Edit Contracts
4. View Contracts
5. Search Contracts

i have done all the 4 options but now im stuck in the 5th option. i have to use checkboxes filters for search and first and the last name fields.

here is my form code

Code: Select all

<form method="post" action="searchCode.php">
First Name: 
<input name="name" type="text" id="name" />
Middle Name: 
<input name="mname" id="mname" type="text" />
Last Name: 
<input name="lname" id="lname" type="text" />
Date:
<input type="text" name="date" id="currentdate" />

<input type="checkbox" name="gender[]" value="male" />Male<br />
<input type="checkbox" name="gender[]" value="female" />Female			
												 						
<input name="desig[]" type="checkbox" value="lecturar"/>Lecturar 
<input type="checkbox" name="desig[]" value="professor" />Professor 
<input type="checkbox" name="desig[]" value="assistantProfessor" />Assistant Professor
<input type="checkbox" name="desig[]" value="associateProfessor" />Associate Professor
<input type="checkbox" name="desig[]" value="researchAssociate" />Research Associate<br />
<input type="checkbox" name="desig[]" value="advisor" />Advisor
<input type="checkbox" name="desig[]" value="HOD" </span>				  

<input name="dept[]" type="checkbox" value="Comuter Science"  />Computer Science
<input type="checkbox" name="dept[]" value="Health Informatics Unit" />Health Informatics Unit
<input name="dept[]" type="checkbox" value="Electrical Engineering" />Electrical Engineering
<input type="checkbox" name="dept[]" value="Management Sciences" />Management Sciences<br />
<input type="checkbox" name="dept[]" value="Mathamatics" />Mathamatics
<input type="checkbox" name="dept[]" value="Physics" />Physics
<input type="checkbox" name="dept[]" value="Bio Sciences" />Bio Sciences
<input type="checkbox" name="dept[]" value="Meteorology" />Meteorology
<input type="checkbox" name="dept[]" value="Architecture" />Architecture

<input name="scales[]" type="checkbox" value="sg1"  />SG-I
<input name="scales[]" type="checkbox" value="sg2"  />SG-II
<input name="scales[]" type="checkbox" value="sg3"  />SG-III
<input name="scales[]" type="checkbox" value="sg4"  />SG-IV
<input name="scales[]" type="checkbox" value="ras" />RA 
<input name="scales[]" type="checkbox" value="og1nphd" />OG-I-
<input name="scales[]" type="checkbox" value="og2nphd" />OG-II-NonPhd
<input name="scales[]" type="checkbox" value="og3nphd" />OG-III-NonPhd
<input name="scales[]" type="checkbox" value="og4nphd" />OG-IV-NonPhd
<input name="scales[]" type="checkbox" value="og2phd" />OG-II-Phd
<input name="scales[]" type="checkbox" value="og3phd" />OG-III-Phd
<input name="scales[]" type="checkbox" value="og4phd" />OG-IV-Phd</p>
now i cant seem to get it in my mind tht how the hell im going to do tht...shud i use ifs and elses, in tht case it can get upto 256 combinations.

and 1 more thing all these things are in one table....plz gyus help me
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: creating the searching in php

Post by social_experiment »

Can a person select more than one option per set of checkboxes? All of your checkboxes will result in arrays of values, which isn't incorrect, but some options, such as gender can only be one option. If only one value can be selected, remove the [] from the name property of the specific input element. If a user picks one only that value will be sent instead of multiple ones which might cause confusion when you start searching.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
hmxa
Forum Newbie
Posts: 4
Joined: Tue Jan 04, 2011 1:15 pm

Re: creating the searching in php

Post by hmxa »

social_experiment wrote:Can a person select more than one option per set of checkboxes? All of your checkboxes will result in arrays of values, which isn't incorrect, but some options, such as gender can only be one option. If only one value can be selected, remove the [] from the name property of the specific input element. If a user picks one only that value will be sent instead of multiple ones which might cause confusion when you start searching.
no tht is the problem, a person can check any checkbox. for example a person can check to see if there are any records of male AND female, and the same condition goes for the rest of the checkboxes.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: creating the searching in php

Post by social_experiment »

hmxa wrote:no tht is the problem, a person can check any checkbox. for example a person can check to see if there are any records of male AND female, and the same condition goes for the rest of the checkboxes.
Ok. Just a question then (and this is purely for curiosity sake) Why not show all the records. IMO with search options this diverse you might as well display all the records and let the user sift through them.

Back to your original question. What would a row in this table look like? If you can give an example of the fields and a value for each one that would help :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
AndrewBliss
Forum Newbie
Posts: 7
Joined: Mon Oct 05, 2009 4:24 pm

Re: creating the searching in php

Post by AndrewBliss »

I threw together some code fast. I will select anything the person checks or enters in. I don't know how efficient it is either.

Code: Select all

$query = 'SELECT columns FROM table';
$query_where = array();

if (isset($_POST['name']) && $_POST['name']) {
	$query_where[] = ' name = "' . $_POST['name'] . '"';
}

if (isset($_POST['mname']) && $_POST['mname']) {
	$query_where[] = ' mname = "' . $_POST['mname'] . '"';
}

if (isset($_POST['lname']) && $_POST['lname']) {
	$query_where[] = ' lname = "' . $_POST['lname'] . '"';
}

if (isset($_POST['date']) && $_POST['date']) {
	$query_where[] = ' date = "' . $_POST['date'] . '"';
}

if (isset($_POST['gender'])) {
	$query_text = ' (';
	foreach ($_POST['gender'] as $index => $gender) {
		if ($index > 0) $query_text .= ' OR ';
		$query_text .= 'gender = "' . $gender . '"';
	}
	$query_text .= ')';
	$query_where[] = $query_text;
}

if (isset($_POST['desig'])) {
	$query_text = ' (';
	foreach ($_POST['desig'] as $index => $desig) {
		if ($index > 0) $query_text .= ' OR ';
		$query_text .= 'desig = "' . $desig . '"';
	}
	$query_text .= ')';
	$query_where[] = $query_text;
}

if (isset($_POST['dept'])) {
	$query_text = ' (';
	foreach ($_POST['dept'] as $index => $dept) {
		if ($index > 0) $query_text .= ' OR ';
		$query_text .= 'dept = "' . $dept . '"';
	}
	$query_text .= ')';
	$query_where[] = $query_text;
}

if (isset($_POST['scales'])) {
	$query_text = ' (';
	foreach ($_POST['scales'] as $index => $scales) {
		if ($index > 0) $query_text .= ' OR ';
		$query_text .= 'scales = "' . $scales . '"';
	}
	$query_text .= ')';
	$query_where[] = $query_text;
}

if (count($query_where) > 0) {
	foreach ($query_where as $index => $statement) {
		if ($index == 0) {
			$query .= ' WHERE ';
		} else {
			$query .= ' OR ';
		}
		$query .= $statement;
	}
} else {
	$query .= 'WHERE 0 = 1'; 
}

echo '<pre>';
echo $query;
echo '</pre>';
hmxa
Forum Newbie
Posts: 4
Joined: Tue Jan 04, 2011 1:15 pm

Re: creating the searching in php

Post by hmxa »

social_experiment wrote:
hmxa wrote:no tht is the problem, a person can check any checkbox. for example a person can check to see if there are any records of male AND female, and the same condition goes for the rest of the checkboxes.
Ok. Just a question then (and this is purely for curiosity sake) Why not show all the records. IMO with search options this diverse you might as well display all the records and let the user sift through them.

Back to your original question. What would a row in this table look like? If you can give an example of the fields and a value for each one that would help :)
well what can i say this is the university project and unfortunately i have to stick to it. if it was my own wish i would've left it ages ago... :D

n btw the table looks like this
id Fname Mname Lname Date gender Designation Department payscale rank
1 jhon smith simpson Jan 02 , 2011 male professor Health Informatics Unit og4phd 20
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: creating the searching in php

Post by social_experiment »

hmxa wrote:well what can i say this is the university project and unfortunately i have to stick to it. if it was my own wish i would've left it ages ago...
Lol yeah i hear you. Been involved with a few of those myself ;) Kudos for sticking it out.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
hmxa
Forum Newbie
Posts: 4
Joined: Tue Jan 04, 2011 1:15 pm

Re: creating the searching in php

Post by hmxa »

AndrewBliss wrote:I threw together some code fast. I will select anything the person checks or enters in. I don't know how efficient it is either.
it worked perfectly fine....thnx a lot mate...u saved ma life :D
Post Reply