Dynamic Multiple dropdown select options from MySQL using PHP - 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).

Related

How to limit the number of columns in a MySQL table when units can have multiple values for a field?

Suppose the unit of analysis is a person. I want to have one column for the name and another column for the language he or she speaks. Some people speak many languages whereas some only speak one. However, I don't want to my table to look like name, language1, language2, language3, language4, language5 because that will produce a lot of empty cells. Ideally I would only have 2 columns but I know it is impossible to shove multiple values into a single cell. Any ideas?
You have a many-to-many relationship, so what you're looking for is known as a bridging table (or associative entity).
What you want is one table called people that represents the individuals, and another table called languages for each language. Each person and each language constitute a separate row in their respective tables.
people:
ID | Name
----------
1 | Aaron
2 | Bob
3 | Clyde
languages:
ID | Name
------------
1 | English
2 | French
3 | Spanish
Then you need to make use of a third table (say spoken_languages), which makes use of foreign keys to tie the two tables together:
person_id | language_id
-----------------------
1 | 1
2 | 1
2 | 2
3 | 3
In the above example:
Aaron only speaks English.
Bob speaks both English and French.
Clyde only speaks Spanish.

How do I use multiple tables to create a user profile?

I made a firm to add a user to my database now I want to have two tables. One table keeps track of the languages the user knows and the other table the design software the user uses.
Would I create 3 tables (profile, languages, software) each with an I'd field and when I add a user add a row to each table?
As you begin to add several many-to-many relationships, you need more tables to 'link' the information together. Here's how I would tackle the problem:
Note The IDs should all be unique indexed columns. Consider using AUTO_INCREMENT.
Table 1: Contains user's profile information
| ProfileID |UserInfo |
|=======================|
| 0 | Info |
|-----------------------|
| 1 | Info2 |
|-----------------------|
Table 2: Stores the possible languages
|LanguageID |LanguageName|
|========================|
| 50 | Python |
|------------------------|
| 51 | Java |
|------------------------|
and so on...
Table 3: Stores the Profile links to the languages
|ProfileID |LanguageID |
|========================|
| 0 | 50 |
|------------------------|
| 0 | 51 |
|------------------------|
| 1 | 50 |
|------------------------|
Every time you wanted to add a language to a user's profile, you'd create an entry in this table.
You would add two more tables for the software a user knows. One table for all the possible types of software, and another to store the links.
When you want to retrieve the information, you would do an operation such as the one below:
SELECT * FROM Table3
LEFT JOIN Table2
ON Table3.LanguageID = Table2.LanguageID
WHERE ProfileID = [TheProfileIDToSearch]
This structure uses JOIN to link tables together to return information from several tables at once. Here is a W3Schools quick explanation about SQL JOINS.

MySQL Database Structure for Employee Position Database

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.

Creating recommendation algorithm base on user interests

I'm currently building an application that would recommend website base on their tag.
On my website when a user registers, it will fill out an interests. So this is a sample interest:
football, model trains, hockey
So this is separated by commas. So when the user clicks on register that will be saved in my database. This is the design of my database.
userID | name | interest
001 | John Doe | sports, model trains, hockey
So on the other hand, I also have users in my sites who uploads website URLs and also creates a tag related to it. So this is my database design for that:
postID | title | tags
001 | techcrunch.com | technology,softwares,startups
002 | nba.com | basketball,sports,all-star
003 | tmz.com | gossip, showbiz
So the logic for this one is that, I wanted to recommend NBA.com to user John Doe since NBA.com has a tag of sports and John Doe's interest has a sports tag.
Do you have any idea how to do that one? Just a follow up question, Is the database design correct or should I create a new table to store all the tags. Something like that (not sure though).
Your help would be greatly appreciated and rewarded! Thanks in advance! :)
I would have normalized the database so that you have tags in a separate table and relationship tables to connect with it. As such:
User table:
UserId Name
001 John Does
TagUserRelation
UserId TagId
001 001
Tag table:
TagId TagName
001 Sports
TagUrlRelation
TagId Url
001 nba.com
001 nhl.com
To increase performance I would have continued by creating indexed views with the necessary joins and implementing stored procedures to work with them.
An alternative, as mentioned, is full text search but this will be much slower and generally not considered good database design in this case.
this can be done by using full text search
refer here
You should create two separate table which hold single tags, several for each person or post.
You can create a multi-column primary key for it if you wish.
userID | interest
001 | sports
001 | model trains
001 | hockey
...
and the same way for posts:
postID | tags
003 | gossip
003 | showbiz
...
This greatly enhances your chances to write efficient SQL.
It would be much better to store the tags separately. So that you have a table for the tags and two more tables - one for the relationship between users and tags, and one for the relationship between posts and tags.
users
----------------------------------------
userId | name | password | ....
1 | John Doe | $p$fgA |
tags
--------------------
tagId | tagname
1 | basketball
2 | hockey
user_interests
----------------------------
id | user_id | tag_id
1 | 1 | 1
2 | 1 | 2
post_tags
--------------------------
id | post_id | tag_id
1 | 1 | 2
Then you use JOINs to get the required information

Yii: Find in Active Record by a PHP Expression

Say you have an Active Record model which contains a set of records:
id | name
---------
1 | Record1
2 | Record2
3 | Record3
Users who has the permission to see each records are stored in another table, using a foreign key to represent the record, in a comma separated way:
foreignId | users
-----------------
1 | joe, doe, zoe
2 | joe
3 | doe, zoe
There is an authentication manager bizRule which checks if current user has the permission to see a record. You give it the record id and it checks the permissions table to see if the user is in the comma separeted field.
Yii::app()->authManager->checkAccess('seeRecord', $id);
Is there an easy way using CActiveRecord to pass a PHP Expression "query"? Something like:
Record::model()->findByPHPExpression('Yii::app()->authManager->checkAccess('seeRecord', array('id' => 'id'));
If the expression returns true for the current record, that record would be added.
Thank you
You have some serious non-yii related issue, your database schema is wrong, please read some about database normalization.
You should have an intermediate table, if a user can see various records, and a record can be seen by various users, then you need an intermediate table.
Users, Users_cansee_Records, Records
The intremediate table will have 2 primary keys, that are the user_id and record_id respectively
for your example this table will have something like:
user | record
--------------
1 | joe
1 | doe
1 | zoe
2 | joe
3 | doe
3 | zoe
Yii supprots this "Many many" relationships out of the box. but please read about database normalization, its an important topic, database design is a critical step in any project development.

Categories