Jump to content

dynamic select options...select selected?


ag3nt42

Recommended Posts

hello all,

 

I'm having a logic problem here

 

I'm working on a wep app where users are allowed to create "Departments" as many as they want to.

 

They are also allowed to create "Employees" as many as they want..

 

when creating an employee you must choose a default department for that employee..

 

Which is fine..

 

after creation the users are then allowed to view the "Details" for the newly created employee.

In the details section.. is the same form as the create section..

 

the only difference is that the forms inputs are now filled in with the information submitted previously.

 

(So the user may update this information)

 

 

My problem is that how do you say in code:

 

List all departments..

 

and select department the user has in the DB (the one setup at creation)..

 

now I know if I had a static list of departments I could simply say

 

if($department=='ThisDepartment')
{
$Department1="SELECTED";
}
else
{
$Department1='';
}

then in the select form I can put this

 

<option $Department1 value='Department1'>Department 1</option>
<option $Department2 value='Department2'>Department 2</option>

 

but how do i do this with dynamic options?

Link to comment
Share on other sites

If you have a table dedicated to only departments like (example):

 

Departments

dept_id  dept_name dept_description etc...

 

You could do something like this:

 

$sql = "select dept_name from departments";
$result = mysql_query($sql);

while ($depts = mysql_fetch_assoc($result)) {
  echo "<option value = '{$depts['dept_name']}'>{$depts['dept_name']}</option>";
}

Link to comment
Share on other sites

I thought of that tho.. the only problem is then how do they choose another department?

 

See it like this

 

First I have a screen where you can setup departments unlimited amount

 

then on the employee's screen you can select a default department for that employee.

 

then after creation of the employee you can view the details and change the details if you like.

 

so there needs to be the whole list of departments in the dropdown.. with the department selected that was chosen during creation.

 

so I can list out the departments just fine.. I can even figure out which department the user selected.

 

my problem is how do I select the selected option.

Link to comment
Share on other sites

here is my loop for the departments on the "Details" screen

<?php
//* GET DEPARMENTS *//
$DepartmentSQL="SELECT * FROM [".$datatable."]";
$DepartmentResult=mssql_query($DepartmentSQL) or die (mssql_get_last_message());
while($row=mssql_fetch_array($DepartmentResult))
{
	echo("<option value='".$row['cat_dep#']."'>".$row['cat_dep#']."  |  ".$row['Description']."</option> ");
}
?>

 

I hope everyone understands what I'm asking.. I kno it is confusing.

 

If I had a static amount of options then I could do this:

<?php

if($Department=='100')
{
   $Selected1='Selected';
}
else
{
   $Selected1='';
}

if($Department=='200')
{
   $Selected2='Selected';
}
else
{
   $Selected2='';
}

echo("
<select name='Departments'>
<option ".$Selected1."  value='100'>Department1</option>
<option ".$Selected2." value='200'>Department2</option>
</select>
");

?>

Link to comment
Share on other sites

I'm trying to think how i can word this correctly..

 

Here is my logic:

 

1st:

Figure out which department was intially selected by the user for that employee

 

2nd:

Generate a select option for each of the departments in the database

 

3rd:

Compare initial selected department agaist each department listed from the database

 

4th:

If match exists select the appropriate select option.

 

 

#PROBLEM#

1st:

How do I compare the intial value agaist a list of variable values to gain a match.

 

2nd:

Once matched how do I then Select the appropriate option from the list?

 

3rd:

While still giving the user the rest of the list of options.. (in case they want to change the department.)

 

*Thoughts*

 

I guess if I could somehow say in SQL

 

SELECT * FROM departsments EXCEPT (The users default department)

 

then I could first get the users department.. select it and then finish the list..

But I don't think you can do that with SQL

 

 

really really stuck on this one guys..

 

thanks in advance I appreciate any suggestions.

Link to comment
Share on other sites

Okay I'm not sure how you have your table(s) setup but assuming you have what, some kind of account info, with a dept_id field in the account_info table, you would "select dept_name from departments where departments.dept_id = account_info.dept_id"  and then pretty much use the same condition as in your OP to mark the current department as selected.

 

// let's just go ahead and assume that you are wanting to select all the info from the account_info
// type table so you already have the current dept_id associated with the account
// and you have the account info stored in an array called $account_info so the current dept_id
// is $account_info['dept_id'] if that's not how you have your script setup then perhaps you need
// to better explain your setup.


// select all of the dept_name and dept_id from departments, because you're going to 
// want to change the dept_id in the account_info table if user selects a new one
$sql = "select dept_id, dept_name from departments";
$result = mysql_query($sql);

while ($depts = mysql_fetch_assoc($result)) {
   // for each iteration of the loop, we compare the current dept_id to the department's dept_id
   // and assign something to $selected based on whether it matches or not. 
   $selected = ($account_info['dept_id'] == $depts['dept_id'])? "SELECTED" : "";
   // pass the dept_id as the value to change, and use the dept_name for readability
   echo "<option value = '{$depts['dept_id']}' $selected>{$depts['dept_name']}</option>";
}

Link to comment
Share on other sites

ok i think i see what your saying

 

btw my db is setup like this

 

I have a table for departments themselves

(departments/categories)

 

Departments
____________
cat_dep#
Description
TimeSlots
Hours
ID (autoincrement)

 

Then I have a table for employee's

 

Employee
____________
ID (autoincrement)
EmpID
FirstName
Initial
LastName
Addy1
Addy2
Phone
Department *
City
State
Zip

 

 

hope that helps.

 

ps(The specific "Piece" of the Department that gets put in the employee's "Department" section is the Department# [aka cat_dep#] )

 

that number is assigned by the user so i have no way of telling what it is.

could be any number from 1-999

Link to comment
Share on other sites

Under the "Employee Table" the column "Department"

 

holds the same value as the value in the column "cat_dep#" under the "Department table"

 

here i have a better Idea..

 

here is my code two pages all together..

 

First page handles the "Creation" and "Deletion" forms

 

The second page handles the "Details" form as well as ALL of the processing for ALL forms.

 

First Page:

<?php
session_start();
require('../../config.php');
/////////////////////////////////
//*****************************//
//***** L.O.T.U.S. AUTHOR *****//  
//****** ag3nt42 ******//
//*****************************//

////////////////////////////////
//* OPEN DATABASE CONNECTION *//
////////////////////////////////
$con = mssql_connect($dblocation,$dbusername,$dbpassword);

if (!$con)
{
die('Could not connect: ' . mssql_error());
}

mssql_select_db($dbname, $con);

$datatable=$tblpre."cat_dep";

//* DISPLAY INTERFACE *//
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='../admin.css' />
</head>
<body>
<table style='width:100%;height:100%;vertical-align:middle;text-align:center;'>
<tr>
	<td colspan='2'><h1>SETUP EMPLOYEES</h1></td>
</tr>
<tr>
	<td colspan='2'><p>Here you can setup unlimited amount of Employees</p></td>
</tr>
<tr>
	<td colspan='2'><hr /></td>
</tr>
<tr>
	<td colspan='2'> </td>
</tr>
<tr>
	<td colspan='2'><hr /></td>
</tr>
<tr>
	<td><h2>CREATE NEW EMPLOYEE</h2></td>
	<td><h2>AVAILABLE EMPLOYEES</h2></td>
</tr>
<tr>
	<td width='48%'>
		<fieldset style='width:75%'><legend>Create New</legend>
		<form action='../Ca_info/Cus_info.php' method='post'>
		<table style='margin-left:25px;'>
			<tr>
				<td width='10%' style='text-align:left;'>
					ID:
				</td>

				<td style='text-align:left;'>
					<input type='text' size='3' maxlength='3' name='EmpID' />
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Department:
				</td>
				<td style='text-align:left;'>
					<select name='Department'>
");
//* LOOP FOR DEPARTMENTS *//

//* GET DEPARMENTS *//
$DepartmentSQL="SELECT * FROM [".$datatable."]";
$DepartmentResult=mssql_query($DepartmentSQL) or die (mssql_get_last_message());
while($row=mssql_fetch_array($DepartmentResult))
{
	echo("<option value='".$row['cat_dep#']."'>".$row['cat_dep#']."  |  ".$row['Description']."</option> ");
}

//END LOOP
echo("
					</select>
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					First Name:
				</td>
				<td style='text-align:left;'>
					<input type='text' name='FirstName' />
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Middle Initial:
				</td>
				<td style='text-align:left;'>
					<input type='text' size='1' maxlength='1' name='Initial' />
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Last Name:
				</td>
				<td style='text-align:left;'>
					<input type='text' name='LastName' />
				</td>
			</tr>
			<tr>
				<td> </td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Address1:
				</td>
				<td style='text-align:left;'>
					<input type='text' name='Addy1' />
				</td>					
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Address2:
				</td>
				<td style='text-align:left;'>
					<input type='text' name='Addy2' />	
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Phone #:
				</td>
				<td style='text-align:left;'>
					<input type='text' name='Phone' /> 
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					City:
				</td>
				<td style='text-align:left;'>
					<input type='text' name='City' />
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					State:
				</td>
				<td style='text-align:left;'>
					<input type='text' maxlength='2' size='2' name='State' />
				</td>
			</tr>
			<tr>
				<td width='10%' style='text-align:left;'>
					Zip:
				</td>
				<td style='text-align:left;'>
					<input type='text' maxlength='5' size='5' name='Zip' />
				</td>
			</tr>
			<tr>
				<td colspan='2'> </td>
			</tr>
			<tr>
				<td colspan='2'>
					<input type='reset' value='Reset' name='action' />
					   
					<input type='submit' value='Create' name='action' />
				</td>
			</tr>
		</table>
		</form>
		</fieldset>
	</td>
	<td width='48%'>
		<fieldset><legend>Available List</legend>
");
////////////////////////////////
//* START LOOP FOR AVAILABLE *//
////////////////////////////////
$datatable=$tblpre."Employee";

//* GATHER AVAILABLE *//
$AvailableSQL="SELECT * FROM [".$datatable."]";
$AvailableResult=mssql_query($AvailableSQL) or die (mssql_get_last_message());
$AvailableCount=mssql_num_rows($AvailableResult);

//* ARE THERE NE AVAILABLE??? *//
if($AvailableCount>0)
{
	//THERE ARE SOME

	echo("
			<form action='../Ca_info/Cus_info.php' method='post'>
				<select multiple name='Employeez[]' size='".$AvailableCount."'>		
	");

	while($row=mssql_fetch_array($AvailableResult))
	{
		echo("<option value='".$row['EmpID']."'>".$row['EmpID']."  |  ".$row['FirstName']."  |  ".$row['Initial']."  |  ".$row['LastName']."</option>".PHP_EOL);		
	}

	echo("
				</select>
				<br /><br />
			<input type='submit' value='Details' name='action' />   <input type='submit' value='Delete' name='action' />
			</form>
	");
}
else
{
	echo("<h3>None Available</h3>");
}

//* END AVAILABLE LOOP *//
echo("
		</fieldset>
	</td>
</tr>
</table>
</body>
</html>
");
?>

 

Second Page :

<?php
session_start();
require('../../config.php');
$Success=0;
$Fail=0;
///////////////////////
//* OPEN CONNECTION *//
///////////////////////
$con = mssql_connect($dblocation,$dbusername,$dbpassword);

if (!$con)
{
die('Could not connect: ' . mssql_error());
}

mssql_select_db($dbname, $con);

$datatable=$tblpre."Employee";

//////////////////
//* GET ACTION *//
//////////////////
if(!(isset($_POST['action'])))
{
$action='';
}
else
{
$action=$_POST['action'];
}

/////////////////////
//* WHICH ACTION? *//
/////////////////////
if($action=='Create')
{	
////////////////////////////
//* HARVEST CREATE INPUT *//
////////////////////////////
if(!(isset($_POST['EmpID']))){$EmpID='';}else{$EmpID=$_POST['EmpID'];}
if(!(isset($_POST['Department']))){$Department='';}else{$Department=$_POST['Department'];}
if(!(isset($_POST['FirstName']))){$FirstName='';}else{$FirstName=$_POST['FirstName'];}
if(!(isset($_POST['Initial']))){$Initial='';}else{$Initial=$_POST['Initial'];}
if(!(isset($_POST['LastName']))){$LastName='';}else{$LastName=$_POST['LastName'];}
if(!(isset($_POST['Addy1']))){$Addy1='';}else{$Addy1=$_POST['Addy1'];}
if(!(isset($_POST['Addy2']))){$Addy2='';}else{$Addy2=$_POST['Addy2'];}
if(!(isset($_POST['Phone']))){$Phone='';}else{$Phone=$_POST['Phone'];}
if(!(isset($_POST['City']))){$City='';}else{$City=$_POST['City'];}
if(!(isset($_POST['State']))){$State='';}else{$State=$_POST['State'];}
if(!(isset($_POST['Zip']))){$Zip='';}else{$Zip=$_POST['Zip'];}

//* SETUP SQL INJECTIONS *//
$CreateSQL="INSERT INTO [".$datatable."] (EmpID, FirstName, Initial, LastName, Addy1, Addy2, Phone, Department, City, State, Zip) VALUES ('".$EmpID."', '".$FirstName."', '".$Initial."', '".$LastName."', '".$Addy1."', '".$Addy2."', '".$Phone."', '".$Department."', '".$City."', '".$State."', '".$Zip."')";
$CreateQuery=mssql_query($CreateSQL) or die(mssql_get_last_message());


//* RUN INJECTIONS *//
if($CreateQuery)
{
	$Success++;//Success
}
else
{
	$Fail++;//FAILED
}
}	

//* ELSE *//

if($action=='Delete')
{
////////////////////////////
//* HARVEST DELETE INPUT *//
////////////////////////////
if(!(isset($_POST['Employeez']))){$Employeez='';}else{$Employeez=$_POST['Employeez'];}

//* HOW MANY *//
$EmpCount=count($Employeez);

//* DECREMENT BY 1 TO MATCH ARRAYS *//
$EmpCount--;

///////////////////////////////
//* SETUP DELETE INJECTIONS *//
///////////////////////////////
///* IN FOR LOOP FOR MULTI *///
///////////////////////////////
for($x=0;$x<=$EmpCount;$x++)
{	
	if($Employeez=='')
	{
		$Fail++;
		$NoEmpsSelected="You have not selected any Employees to delete!";
	}
	else
	{
		$NoEmpsSelected='';

		//INJECT QUERY
		$EmpDelete[$x]="DELETE FROM [".$datatable."] WHERE EmpID='".$Employeez[$x]."'";

		//RUN INJECT
		$EmpQuery[$x]=mssql_query($EmpDelete[$x]) or die (mssql_get_last_message());

		if($EmpQuery[$x])
		{
			$Success++;//SUCCESS
		}
		else
		{
			$FAIL++;//FAILED
		}
	}
}
}

//* ELSE *//

if($action=='Details')
{
/////////////////////////////
//* HARVEST DETAILS INPUT *//
/////////////////////////////
if(!(isset($_POST['Employeez']))){$Employeez='';}else{$Employeez=$_POST['Employeez'];}

//* HOW MANY EMPZ? *//
$EmpCount=count($Employeez);

//* DECREMENT BY 1 TO MATCH ARRAYS *//
$EmpCount--;

//* CHANGE DATATABLE *//
$datatable=$tblpre."cat_dep";

///////////////////////////////////
//* HARVEST DEPARTMENTS FROM DB *//
///////////////////////////////////
$DepartmentSQL="SELECT * FROM [".$datatable."]";
$DepartmentResult=mssql_query($DepartmentSQL) or die (mssql_get_last_message());


//* RESET DATATABLE *//
$datatable=$tblpre."Employee";

///////////////////////////////
//* START DETAILS INTERFACE *//
///////////////////////////////
echo("
	<table style='width:100%;height:100%;text-align:center;vertical-align:middle;'>
		<tr>
			<td>
				<form action='' method='post'>
");

////////////////////////////
//* START LOOP FOR MULTI *//
////////////////////////////
for($x=0;$x<=$EmpCount;$x++)
{
	////////////////////////////////
	//* HARVEST DB INFO FOR EMPz *//
	////////////////////////////////
	$DetailsSQL[$x]="SELECT * FROM [".$datatable."] WHERE EmpID='".$Employeez[$x]."'";
	$DetailsResult[$x]=mssql_query($DetailsSQL[$x]) or die (mssql_get_last_message());
	while($row=mssql_fetch_array($DetailsResult[$x]))
	{
		$ID[$x]=$row[0];

		$EmpID[$x]=$row[1];

		$FirstName[$x]=$row[2];

		$Initial[$x]=$row[3];

		$LastName[$x]=$row[4];

		$Addy1[$x]=$row[5];

		$Addy2[$x]=$row[6];

		$Phone[$x]=$row[7];

		$Department[$x]=$row[8];

		$City[$x]=$row[9];

		$State[$x]=$row[10];

		$Zip[$x]=$row[11];
	}

	 $Selected = ($Department[$x] == $row['cat_dep#'])? "SELECTED" : "";

	//////////////////////////////////
	//* CONTINUE DETAILS INTERFACE *//
	//////////////////////////////////
	echo("

		<fieldset><legend>".$EmpID[$x]."</legend>
			<table>
				<tr>
					<td>
						ID:						
					</td>
					<td>
						<input type='text' value='".$EmpID[$x]."' name='UEmpID[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Department:
					</td>
					<td>
						<select name='UDepartment[".$x."]'>
	");

						while($row=mssql_fetch_array($DepartmentResult[$x]))
						{
							echo("<option ".$Selected." value='".$row['cat_dep#']."'>".$row['cat_dep#']."  |  ".$row['Description']."</option> ");
						}

	echo("
						</select>
					</td>
				</tr>
				<tr>
					<td>
						First Name:
					</td>
					<td>
						<input type='text' value='".$FirstName[$x]."' name='UFirstName[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Middle Initial:
					</td>
					<td>
						<input type='text' size='1' maxlength='1' value='".$Initial[$x]."' name='UInitial[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Last Name:
					</td>
					<td>
						<input type='text' value='".$LastName[$x]."' name='ULastName[".$x."]' />
					</td>
				</tr>
				<tr>
					<td colspan='2'> </td>
				</tr>
				<tr>
					<td>
						Address 1:
					</td>
					<td>
						<input type='text' value='".$Addy1[$x]."' name='UAddy1[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Address 2:
					</td>
					<td>
						<input type='text' value='".$Addy2[$x]."' name='UAddy2[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Phone #:
					</td>
					<td>
						<input type='text' size='10' maxlength='10' value='".$Phone[$x]."' name='UPhone[".$x."]' />
					</td>	
				</tr>
				<tr>
					<td>

					</td>
					<td>

					</td>
				</tr>
			</table>
		</fieldset>

	");
}
}

//* ELSE *//

if($action=='Update')
{
//DO UPDATE
}

////////////////////
//* SUCCESSFULL? *//
////////////////////
if($Success>0)
{
//Successful
echo("<table style='width:100%;height:100%;vertical-align:middle;text-align:center;'><tr><td><font color='green'><h1>Employee ".$action." Successful! x".$Success."</h1></font><br />Redirecting...</td></tr></table>");
echo("<meta http-equiv='refresh' content='1.5;url=http://".$domain.$sPath."admin/Edits/Employee.php'>");
}
elseif($Fail>0)
{
//FAILED
echo("
	<table style='width:100%;height:100%;vertical-align:middle;text-align:center;'>
		<tr>
			<td>
				<font color='red'>
				<h1>Employee ".$action." FAILED! x".$Fail."</h1>".mssql_get_last_message()."</font>
");
				if($action=='Delete'){echo("<br /><br />".$NoEmpsSelected);}
				if($action=='Create'){echo('');}
echo("
			</td>
		</tr>
	</table>
");


}



?>

Link to comment
Share on other sites

Okay so yeah...in your script where the user gets to edit info, including the department, you would initially be basically "select * from employee where EmpID = 'x'" for display/edit, right? So since you do that, you have the Employee.Department value from that already, when you get down to the part where you need the drop down of departments, you would use that code, except change the columns/table to your specific names.

 

 

Link to comment
Share on other sites

i don't understand this part that you said

 

 

when you get down to the part where you need the drop down of departments, you would use that code, except change the columns/table to your specific names.

 

 

but yes I already get the value of which department is assigned to the employee once I pull the details for that employee.

 

I just can't figure out how to say

 

Out of all of these departments select only the department which matches the one assigned to the employee yet list the rest of the departments for choosing.

 

could I just do like this

 

while($row=mssql_fetch_array($DepartmentResult[$x]))
{
   if($row['cat_dep#']==$row['Department'])
   {
      echo(//SELECTED SELECT OPTION)
   }
    else
   {
      echo(//SELECT OPTIONS);
   }
}

Link to comment
Share on other sites

Well look at the code in the while loop:

$selected = ($account_info['dept_id'] == $depts['dept_id'])? "SELECTED" : "";
// pass the dept_id as the value to change, and use the dept_name for readability
echo "<option value = '{$depts['dept_id']}' $selected>{$depts['dept_name']}</option>";

 

Each time the while loop iterates, $selected is going to be assigned something. It will either be assigned "SELECTED" or "".  If the department id of the department being listed matches the current user's department id, then it will be assigned "SELECTED" if it doesn't, $selected = "".  Then just put the $selected in your option tag and the only option tag that will have the SELECTED one is the current user's department.

Link to comment
Share on other sites

ok.. I understand your logic but now i've gone and confused myself all to heck.

 

and when I select more then one employee to view/edit the details for.. only the first employee gets the dropdown.

 

I think I need a cig break. lol :-\

Link to comment
Share on other sites

OK I think its working!!!!!!!!!!! :o

 

 

here is the updated script:

 

<?php
session_start();
require('../../config.php');
$Success=0;
$Fail=0;
///////////////////////
//* OPEN CONNECTION *//
///////////////////////
$con = mssql_connect($dblocation,$dbusername,$dbpassword);

if (!$con)
{
die('Could not connect: ' . mssql_error());
}

mssql_select_db($dbname, $con);

$datatable=$tblpre."Employee";

//////////////////
//* GET ACTION *//
//////////////////
if(!(isset($_POST['action'])))
{
$action='';
}
else
{
$action=$_POST['action'];
}

/////////////////////
//* WHICH ACTION? *//
/////////////////////
if($action=='Create')
{	
////////////////////////////
//* HARVEST CREATE INPUT *//
////////////////////////////
if(!(isset($_POST['EmpID']))){$EmpID='';}else{$EmpID=$_POST['EmpID'];}
if(!(isset($_POST['Department']))){$Department='';}else{$Department=$_POST['Department'];}
if(!(isset($_POST['FirstName']))){$FirstName='';}else{$FirstName=$_POST['FirstName'];}
if(!(isset($_POST['Initial']))){$Initial='';}else{$Initial=$_POST['Initial'];}
if(!(isset($_POST['LastName']))){$LastName='';}else{$LastName=$_POST['LastName'];}
if(!(isset($_POST['Addy1']))){$Addy1='';}else{$Addy1=$_POST['Addy1'];}
if(!(isset($_POST['Addy2']))){$Addy2='';}else{$Addy2=$_POST['Addy2'];}
if(!(isset($_POST['Phone']))){$Phone='';}else{$Phone=$_POST['Phone'];}
if(!(isset($_POST['City']))){$City='';}else{$City=$_POST['City'];}
if(!(isset($_POST['State']))){$State='';}else{$State=$_POST['State'];}
if(!(isset($_POST['Zip']))){$Zip='';}else{$Zip=$_POST['Zip'];}

//* SETUP SQL INJECTIONS *//
$CreateSQL="INSERT INTO [".$datatable."] (EmpID, FirstName, Initial, LastName, Addy1, Addy2, Phone, Department, City, State, Zip) VALUES ('".$EmpID."', '".$FirstName."', '".$Initial."', '".$LastName."', '".$Addy1."', '".$Addy2."', '".$Phone."', '".$Department."', '".$City."', '".$State."', '".$Zip."')";
$CreateQuery=mssql_query($CreateSQL) or die(mssql_get_last_message());


//* RUN INJECTIONS *//
if($CreateQuery)
{
	$Success++;//Success
}
else
{
	$Fail++;//FAILED
}
}	

//* ELSE *//

if($action=='Delete')
{
////////////////////////////
//* HARVEST DELETE INPUT *//
////////////////////////////
if(!(isset($_POST['Employeez']))){$Employeez='';}else{$Employeez=$_POST['Employeez'];}

//* HOW MANY *//
$EmpCount=count($Employeez);

//* DECREMENT BY 1 TO MATCH ARRAYS *//
$EmpCount--;

///////////////////////////////
//* SETUP DELETE INJECTIONS *//
///////////////////////////////
///* IN FOR LOOP FOR MULTI *///
///////////////////////////////
for($x=0;$x<=$EmpCount;$x++)
{	
	if($Employeez=='')
	{
		$Fail++;
		$NoEmpsSelected="You have not selected any Employees to delete!";
	}
	else
	{
		$NoEmpsSelected='';

		//INJECT QUERY
		$EmpDelete[$x]="DELETE FROM [".$datatable."] WHERE EmpID='".$Employeez[$x]."'";

		//RUN INJECT
		$EmpQuery[$x]=mssql_query($EmpDelete[$x]) or die (mssql_get_last_message());

		if($EmpQuery[$x])
		{
			$Success++;//SUCCESS
		}
		else
		{
			$FAIL++;//FAILED
		}
	}
}
}

//* ELSE *//

if($action=='Details')
{
/////////////////////////////
//* HARVEST DETAILS INPUT *//
/////////////////////////////
if(!(isset($_POST['Employeez']))){$Employeez='';}else{$Employeez=$_POST['Employeez'];}

//* HOW MANY EMPZ? *//
$EmpCount=count($Employeez);

//* DECREMENT BY 1 TO MATCH ARRAYS *//
$EmpCount--;

///////////////////////////////
//* START DETAILS INTERFACE *//
///////////////////////////////
echo("
	<table style='width:100%;height:100%;text-align:center;vertical-align:middle;'>
		<tr>
			<td>
				<form action='' method='post'>
");

////////////////////////////
//* START LOOP FOR MULTI *//
////////////////////////////
for($x=0;$x<=$EmpCount;$x++)
{

	//* CHANGE DATATABLE *//
	$datatable=$tblpre."cat_dep";

	///////////////////////////////////
	//* HARVEST DEPARTMENTS FROM DB *//
	///////////////////////////////////
	$DepartmentSQL[$x]="SELECT * FROM [".$datatable."]";
	$DepartmentResult[$x]=mssql_query($DepartmentSQL[$x]) or die (mssql_get_last_message());

	//* RESET DATATABLE *//
	$datatable=$tblpre."Employee";

	////////////////////////////////
	//* HARVEST DB INFO FOR EMPz *//
	////////////////////////////////
	$DetailsSQL[$x]="SELECT * FROM [".$datatable."] WHERE EmpID='".$Employeez[$x]."'";
	$DetailsResult[$x]=mssql_query($DetailsSQL[$x]) or die (mssql_get_last_message());
	while($row=mssql_fetch_array($DetailsResult[$x]))
	{
		$ID[$x]=$row[0];

		$EmpID[$x]=$row[1];

		$FirstName[$x]=$row[2];

		$Initial[$x]=$row[3];

		$LastName[$x]=$row[4];

		$Addy1[$x]=$row[5];

		$Addy2[$x]=$row[6];

		$Phone[$x]=$row[7];

		$Department[$x]=$row[8];

		$City[$x]=$row[9];

		$State[$x]=$row[10];

		$Zip[$x]=$row[11];
	}

	//////////////////////////////////
	//* CONTINUE DETAILS INTERFACE *//
	//////////////////////////////////
	echo("

		<fieldset><legend>".$EmpID[$x]."</legend>
			<table>
				<tr>
					<td>
						ID:						
					</td>
					<td>
						<input type='text' value='".$EmpID[$x]."' name='UEmpID[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Department:
					</td>
					<td>
						<select name='UDepartment[".$x."]'>
	");

						while($row=mssql_fetch_array($DepartmentResult[$x]))
						{
							if($Department[$x]==$row['cat_dep#'])
							{
								echo("<option SELECTED value='".$row['cat_dep#']."'>".$row['cat_dep#']."  |  ".$row['Description']."</option> ");
							}
							else
							{
								echo("<option value='".$row['cat_dep#']."'>".$row['cat_dep#']."  |  ".$row['Description']."</option> ");
							}
						}

	echo("
						</select>
					</td>
				</tr>
				<tr>
					<td>
						First Name:
					</td>
					<td>
						<input type='text' value='".$FirstName[$x]."' name='UFirstName[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Middle Initial:
					</td>
					<td>
						<input type='text' size='1' maxlength='1' value='".$Initial[$x]."' name='UInitial[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Last Name:
					</td>
					<td>
						<input type='text' value='".$LastName[$x]."' name='ULastName[".$x."]' />
					</td>
				</tr>
				<tr>
					<td colspan='2'> </td>
				</tr>
				<tr>
					<td>
						Address 1:
					</td>
					<td>
						<input type='text' value='".$Addy1[$x]."' name='UAddy1[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Address 2:
					</td>
					<td>
						<input type='text' value='".$Addy2[$x]."' name='UAddy2[".$x."]' />
					</td>
				</tr>
				<tr>
					<td>
						Phone #:
					</td>
					<td>
						<input type='text' size='10' maxlength='10' value='".$Phone[$x]."' name='UPhone[".$x."]' />
					</td>	
				</tr>
				<tr>
					<td>

					</td>
					<td>

					</td>
				</tr>
			</table>
		</fieldset>

	");
}
}

//* ELSE *//

if($action=='Update')
{
//DO UPDATE
}

////////////////////
//* SUCCESSFULL? *//
////////////////////
if($Success>0)
{
//Successful
echo("<table style='width:100%;height:100%;vertical-align:middle;text-align:center;'><tr><td><font color='green'><h1>Employee ".$action." Successful! x".$Success."</h1></font><br />Redirecting...</td></tr></table>");
echo("<meta http-equiv='refresh' content='1.5;url=http://".$domain.$sPath."admin/Edits/Employee.php'>");
}
elseif($Fail>0)
{
//FAILED
echo("
	<table style='width:100%;height:100%;vertical-align:middle;text-align:center;'>
		<tr>
			<td>
				<font color='red'>
				<h1>Employee ".$action." FAILED! x".$Fail."</h1>".mssql_get_last_message()."</font>
");
				if($action=='Delete'){echo("<br /><br />".$NoEmpsSelected);}
				if($action=='Create'){echo('');}
echo("
			</td>
		</tr>
	</table>
");


}



?>

 

could you post a link to a tut on this kind of ifelse

 

<?php

$selected = ($account_info['dept_id'] == $depts['dept_id'])? "SELECTED" : "";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.