Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Admin page dropdown

Status
Not open for further replies.

Pegasus101

IS-IT--Management
Aug 1, 2014
8
0
0
GB
I am trying to get something working and would appreciate some help.

Let's say I have 2 tables with a Join;

Employee table

EmployeeIDPK
EmployeeName
DepartmentID(FK)

Department Table

DepartmentID(PK)
DepartmentName

The List table will show the Employee details, including Department Name.
When the 'Edit' button next to the Employee is clicked, the Edit page will open up the Employee Details, including the dropdown with the correct Department Selected. This Department value may be altered by selecting another Department and Submitting.
I cannot get the code right to show the Edit page to show the Dropdown with the right department selected or anything further such as being able to change departments.
I would appreciate help on this. Let me know if you want clarification on any of the points or any more info.

Many thanks.
R
 
I cannot get the code right

From the example you shared, it looks like you are missing a semicolon at the end of a line. [bigsmile]

Yes, clarification is certainly needed.
 
Well without any code its hard to say, the basic idea is to have the currently selected department id available when you are building the dropdown so that you can compare the option being created with the one you want to select.

something like:

Code:
$currentdepartment = [selected department id];

echo "<select name='departmentdropdown'>"
foreach($departmentsRows as $department)
{
[indent]if($department['id']==$currentdepartment)[/indent]
[indent]{[/indent]
[indent][/indent][indent][/indent][indent]echo "<option value='$[department id]' [b][COLOR=#A40000]selected='selected'[/color][/b]>$[departmentName]</option>[/indent]
[indent][/indent][indent]}[/indent]
[indent][/indent][indent]else[/indent]
[indent]{[/indent]
[indent][/indent][indent][/indent][indent]echo "<option value='$[department id]'>$[departmentName]</option>[/indent]
[indent]}[/indent]
}


As to changing department, just update the row with the new department. Again keeping the Id of the row somewhere in your row so you can perofma query with it.

UPDATE table set department=[value from form] where id=rowid



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
spamjim and vacunita, thanks for your help.

I am pretty much a beginner with PHP/mysql. I am writing an update form for the employee details.

Having problems updating the departmentID.

The code that updates is below. My thanks in advance for any help in showing me where I am going wrong. On selecting the new value in the dropdown, the updated value in the employee table is 0.

Thanks.

<tr>
<td>Department:</td>
<td>
<select name="department">
<option value="deptid" label="departmentname" SELECTED><?php echo $row['departmentname']?></option>
<?php
include('db.php');
$sql=mysql_query("select deptid,departmentname from department");
while($row=mysql_fetch_array($sql))
{
$id=$row['deptid'];
$data=$row['departmentname'];
echo '<option value="'.$deptid.'">'.$data.'</option>';
} ?>
</select>
</td>
</tr>
 
Two lines you should look at carefully, .....


Line one >>>>> $id=$row['deptid'];

Line two >>>>> echo '<option value="'.$deptid.'">'.$data.'</option>';




Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Is anyone able to shed any more light on why I can't get this working?

Thanks.
 
Have you rectified the errors in the lines I pointed out?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Chris,

Yes, have looked at the lines. There must clearly be an error there, as you pointed out. But as I have said, I cannot spot it. If you know where the error lies and can let me know the correct code, that would be great and I would be obliged.

RR
 
you're not seeing the woods for the trees.

in the first line you tell us that the variable is deptid and is stored in that key of the $row array. you set this to $id for convenience.
then in the next line you reference $deptid. not $row['deptid'] or $id.
if you had error display and reporting turned on this would have been evident (at least in the source html output). it's a really good idea to do so when developing. Best way is to turn them on in php.ini. but if you cannot then this works in code for some errors

Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
?>

this won't pick up syntax errors in the same script (but will in any included scripts).
 
Okay


You initialise a variable THEN use the name of the column it represents instead.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
In case its not immediately evident:

Code:
[b]$id=$row['deptid'];[/b]
$data=$row['departmentname'];
echo '<option value="'.[b]$deptid[/b].'">'.$data.'</option>';

Where is $deptid coming from?

As has been suggested above having errors displayed would help you a lot when developing as it would have told you that $deptid is not defined anywhere in your code.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top