MySQL Database Structure for Employee Position Database - php

Basically, am working with a Human Resource Management application.
Positions (like trainer, deputy manager, manager, etc) are filled up based on their service.
There is a list of all employees, the one with highest service on any given date occupies the top-most position.
The number of key positions are fixed. Based on the employee's position in the list, his probable position will be displayed.
The positions are like:
(Position - Number of Posts)
Director - 1
Manager - 3
Dy. Manager - 5
Trainer - 10
Now, in the list of employees populated based on their experience, the first in the list will be the Director, next three will be Managers, following 5 employees will be Dy. Managers and next ten would be Trainers.
I want to design a database table for storing the positions and associated numbers. There may be possibility that new positions will be added and also, the number of positions may change with time. Example, if the number of Director posts are increased to two from one, it should be edited. As soon as the number of Director posts are edited, the first two employees in the list will be designated as probable Directors. The same logic holds good to all the other positions too.
Am not sure what should be the database structure for such a table. After done, I should be able to query the table using php for probable position of any given employee based on his rank in the employees list.
Example, if an employee is in rank 5, I should be able to query the table and get the probable position. In this case, rank 5 will be Dy. Manager.
Hope I have made my requirement clear. Please help me in designing a database table for this purpose.
Thank you!

Employees
=========
name | Boss
rank | 1
doj | 2010-01-01
"Select name from Employees order by rank, doj"
doj = date of joining
Is that what you mean?
rank could join to another table, where you store the readable rank title in order to generate title, name, etc

The existing employee database table consists of:
Employees
=========
name | Mark
doj | 2010-01-01
wrk | yes
"Select name from Employees where wrk=yes order by doj"
(this will remove employees in database who has left the company, with status wrk = no)
Lets say this will generate the results something like this:
Mark | 2010-01-01
Steve | 2010-02-01
Jack | 2010-04-01
Rick | 2010-09-01
David | 2010-12-01
Now, when I fetch the results and assign position based on their seniority (assuming positions manager=1, dy. manager=3, trainer=1) the result would be:
Rank | Name | Doj | Designation
1 | Mark | 2010-01-01 | Manager
2 | Steve | 2010-02-01 | Dy. Manager
3 | Jack | 2010-04-01 | Dy. Manager
4 | Rick | 2010-09-01 | Dy. Manager
5 | David | 2010-12-01 | Trainer
After some days, lets assume Jack quits the company (his wrk status will be no). So, the fetched results would look:
Rank | Name | Doj | Designation
1 | Mark | 2010-01-01 | Manager
2 | Steve | 2010-02-01 | Dy. Manager
3 | Rick | 2010-09-01 | Dy. Manager
4 | David | 2010-12-01 | Dy. Manager
As Jack quits, David gets promoted. The rank is just the serial number generated based on the number of rows fetched, auto increment.
Now, I wanted to create a table for the designation and number of positions, so that based on the rank, it fetches the designation.

Related

Dynamic Multiple dropdown select options from MySQL using PHP

I'm trying to come up with some sort solution to a problem where I have to provide a user with dynamic dropdowns depending on the options they choose.
Currently I have 3 tables that are normalized as such.
Currently this works well with my HTML select elements, where if I select John Doe I would get Paul, Kevin and Dick as my second options and if I were to choose Kevin I would get Drake and Kanye as a third option.
My issue is that I do not want to keep creating tables since I would like to add more layers of staff_level in my application.
How would I approach this and have a fully dynamic table structure using PHP and MySQL?
Thank you for taking your time to read this.
You want an association table between the people. Put all of them in one table with unique IDs like so:
Table Staff
id | Name | <Other fields>
----+-------------+----------
1 | John Doe |
2 | Sam Smith |
3 | John Johns |
4 | Paul Pete |
5 | Kevin Mayor |
6 | Dick Ross |
...
Then the association table named whatever you like - maybe StaffHeirarchy:
Table StaffRelationships
id | ManagerId | SubordinateId
---+-----------+--------------
* | Null | 1 # Has no manager
* | 2 | 6 # Dick Ross is subordinate to Sam Smith
This table should have an id field for unique keys, but you don't have to care about what it is (it's not used as a Foreign Key as the Staff.id field is), which is why I put * there - in reality it would be some integer id.
I haven't seen your PHP for pulling values out of the database, but it is basically the same - query the association table filtering for the id of the manager you are looking for and you will get the ids of the subordinates (which you can JOIN on the staff table to get the names).

Finding blank cells in a specific row with php/mySQL

I am attempting at making a site that has a polling section in it. The users need to be able to take previous polls if they have not voted in them yet. So for each poll they take, it adds a note into the cell.
The table looks something like this:
userid | poll1 | poll2 | poll3 | poll4 /
---------+--------+--------+-------+--------/
001 | Y | Y | | /
---------+--------+--------+-------+--------/
002 | Y | | Y | /
--------------------------------------------/
So now, the code would have to select poll 3 and 4 for user 001, and present that to the user on the page. I have been trying a couple different approaches to this but can't seem to find the right code.
I have looked for something for help online and haven't found anything that addresses this issue.
Users
id | name
---+-------
1 | tyrion
2 | cersei
Polls
id | name
---+-------
1 | first poll
2 | second poll
UserPolls
user_id | poll_id
--------+-------
1 | 1
1 | 2
2 | 2
In the above table structure, you have the two main object tables, and one relational table, called UserPolls. This will scale pretty well, for any number of polls you want.
Then, if you want to know which polls were taken by a given user, you can do:
SELECT poll_id FROM UserPolls WHERE user_id = 1
Or if you want the opposite. Polls not taken by a given user:
SELECT id FROM Polls WHERE Polls.id NOT IN (SELECT poll_id FROM UserPolls WHERE user_id = 1)
Note: this is not tested code, but you get the general idea on how to go about designing and normalizing your tables.

Tagging hierarchy and search

I am trying to create a relatively simple hierarchical tagging system that can be searched. Here's how it works as of now, this is the MySQL table structure:
--------------------------------------------
id | tag | parentID | topParentID |
--------------------------------------------
1 | Boston | NULL | NULL |
--------------------------------------------
2 | Events | 1 | 1 |
--------------------------------------------
3 | June 30th | 2 | 1 |
--------------------------------------------
4 | NYC | NULL | NULL |
--------------------------------------------
5 | Attractions | 4 | 4 |
--------------------------------------------
So, if a user types Boston in the search bar, they will be delivered the suggestions "Boston Events" and "Boston Events June 30th". Similarly, if they type NYC in the search bar, they will be delivered "NYC Attractions" as a suggestion.
Also, if someone typed Events into the search bar, they would get the suggestion "Boston Events" or if they typed June 30th, they would get the suggestion "Boston Events June 30th"
I've messed around with code to do this, and I can definitely break the query string into keywords then search the tag table for each of the keywords and return matches, but I have not found the correct way to return the full tag strings in the format I mentioned above.
Well, you can join the same table twice. Suppose, we have $id - id of the current tag:
SELECT
tags.id,
tags.tag,
parent_tags.id,
parent_tags.tag,
parent2_tags.id,
parent2_tags.tag,
FROM
tags
INNER JOIN
tags AS parent_tags
ON
tags.parentID = parent_tags.id
INNER JOIN
tags AS parent2_tags
ON
tags.topParentID = parent2_tags.id
WHERE
tags.id=$id
But it will give parents and grandparents twice because of the incorrect data in your table: parent.id = parent2.id
Actually, this is a very primitive solution, allowing only 2 levels of hierarchy to be displayed in 1 request. If you want to implement any levels, read about nested sets on stack. And there is a great book: "Trees and hierarchies in SQL for smarties" by Joe Celko
I think that you may delete the topParentID column and add one called "level" (Boston would have level 0, events level 1, June 30th level 2).
So you cold order by this level column and implode the values so you would have something like what you wish.
You can do that without the level column, but I think it will be a lot more work on the php side.

How to use data from a different table to obtain the appropriate records in MySQL

I have currently got a PHP generated calendar displaying some holidays for users. This information is stored in a database, I.e holidays and users. I want a user to be able to select a department and then AJAX will load the holidays for users only in that department.
Here are two made up tables with the same fundamental structure:
Table users
+------------------------------------+
| User | Department |
|------------+-----------------------|
| Brian | Sales |
| Tony | Marketing |
| Carol | Marketing |
| Dave | Warehouse |
| Chris | Warehouse |
+------------------------------------+
Table holiday
+------------------------------------+
| ID | User |
|------------+-----------------------|
| 1 | Dave |
| 2 | Tony |
| 3 | Tony |
| 4 | Chris |
| 5 | Carol |
+------------------------------------+
My current query:
$getAllHols = $con->query("SELECT * FROM `holiday`");
So of course, this just gets all holiday. I'm knowledgable enough on PHP to get a list of users in a specific department and then use that in another query to get holidays for those users. But I don't want to do this. I'm thinking there MUST be a pure-SQL solution. So I need the query to get all records from holiday where the user is in the selected department, using the two tables.
I.E:
If I chose the department "Marketing", records 2, 3 and 5 would be returned. (Tony and Carol are in Marketing).
Very easy problem for an advanced SQL user, I'm sure, but not to me. Can anyone help? Thanks.
Try this.
SELECT * FROM users
LEFT JOIN holiday ON users.user = holiday.user
WHERE holiday.department = 'marketing'
As far as I got...
select user
from users inner join holiday
on users.user = holiday.user
where department = 'Marketing'
This would provide a distinct list of records from the Holiday table if there are any matching records from the Users table. This improves upon the option of joining the tables, as you would not have to worry about de-duping the resulting data.
select distinct h.id, h.user
from holiday h
where h.user in (select u.user
from user u
where u.department = 'Marketing')
;

MySQL Update one table, but use data from two other tables as part of the update

I wish to update one table in my database, the data is from a php POST. (It is a page where multiple edits on rows can take place at once, then it processes them all at once after) and i want it so for each "row" or "loop", it builds a single query that can update all the rows at once.
What i want to do, is in the query, select data from two other tables.
E.g
Posted data:
- Task = "Check current Sponsors"
- User Assigned = "Dan"
- Start Meeting = "Mar 1st"
- Meetings Required = 2
And for User Assigned, i want it to basically do this query:
SELECT id FROM team WHERE fullname LIKE 'Dan'
And for the start meeting, i want it to do this query:
SELECT id FROM meetings WHERE starttime='".strtotime("Mar
1st")."'
-- strtotime() makes a unix timestamp from a string.
but i want it to do that for each "task" that gets submitted. (It is queued up via javascript and it sends them all into the same post request)
Anyone have any ideas on how to do this?
Thanks in advance
Table Structures:
Tasks:
id | startmid | length | task | uid | completed
1 | 2 | 1 | Check Sponsors | 1 | 0
Meetings: (Joined by startmid)
id | maintask | starttime | endtime
1 | Sponsors | 1330007400 | 1330012800
Team: (Joined by uid)
id | fullname | position | class | hidden
1 | Team | All Members | black | 0
2 | Dan S | Team Manager | green | 0
you can use the following construct:
UPDATE mytable( col1, col2 )
SELECT col1_val, col2_val
FROM someothertables
WHERE cond1 = cond1;

Categories