how to insert Hierarchical Data respective to ID.
hi friends,
I am new with php, and i m learning a lot from the internet..
I am trying with some issues here and it doesnt work for me..
It like i have a form, where the admin can create a company >>on click submit a company name has been created..I have another form, where it has create users >>
in this form i have a drop down list in which the company name from the database table that has been previously stored is been populated. we need to select the company name from the drop down list and then add the users.. the company can have many users..
eg: the structure looks like :
Company = C, Users = U
C1 - U1,..
ID - 01
C2 - U1, U2, U3,..
ID - 01, 02, 03,..
C3 - U1, U2,..
ID - 01, 02,..
In the second form(create user form) i have users ID field its like when C1 user is saving it shud be, 01, 02...
When C2 user is saving it has to be 01, 02,...and so on...for indivitual company...
How do i set the id.. ? For now is, I have the id increament is like 01, 02, 03... on whatever Company i choose from the drop down list..
Help me with this ? pls.. and thanks a lot...
Shaggy posted this at 21:40 — 2nd March 2010.
They have: 121 posts
Joined: Dec 2008
I'm not sure I understand what you are asking?
If you are attempting to store companies, and their related users, I'd go with a simple parent/child relationship.
Consider:
create sequence seq_companies;
create table companies (
company_id int not null primary key default nextval('seq_companies'),
company_name
);
create sequence seq_company_users;
create table company_users (
company_id int not null,
user_id int not null primary key default nextval('seq_users'),
user_name varchar(100),
CONSTRAINT "company_to_users" FOREIGN KEY ("company_id") references companies ("company_id") ON DELETE CASCADE ON UPDATE CASCADE
);
Insert a company into the companies table.
Insert users into the company_users table. When inserting a user, insert their company_id as well.
This would only provide one level of 'heirarchy' however - there are companies, and then there are users. If you want to get a 'deeper' structure, you'll need to look at "Nested Sets", or consider storing your heirarchical data in a different type of store than RDBMS, such as LDAP.
Cheers,
Shaggy.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.