I need a bit of a nudge on a design I haven't been able to get right. I am receiving an xml doc which I parse into an array with the resulting example elements:
[0] => Array
(
[id] => 0D1XZ6QKN166HC6ANXB7
[organization_id] => 2T93DOPGN9Z7FTNLS21V
[firstname] => Shan
[lastname] => Jahan
[phonenumber] => (111) 222-3333
[email] => sjahan@tj.com
[logininfo_roles_role_name] => Records
[logininfo_roles_role_id] => 1000
[logininfo_roles_role_name2] => Manager
[logininfo_roles_role_id2] => 2000
[logininfo_roles_role_name3] => User
[logininfo_roles_role_id3] => 3000
[logininfo_roles_role_name4] => Seller
[logininfo_roles_role_id4] => 4000
)
[1] => Array
(
[id] => 0D1XZ6QKN166HC6ANXB7
[organization_id] => 2T93DOPGN9Z7FTNLS21V
[firstname] => Roland
[lastname] => Oliver
[phonenumber] => (111) 222-3333
[email] => roliver@tj.com
[logininfo_roles_role_name] => Records
[logininfo_roles_role_id] => 1000
[logininfo_roles_role_name2] => Manager
[logininfo_roles_role_id2] => 2000
[logininfo_roles_role_name3] => User
[logininfo_roles_role_id3] => 3000
[logininfo_roles_role_name4] => Seller
[logininfo_roles_role_id4] => 4000
[logininfo_roles_role_name5] => Lowly Coder
[logininfo_roles_role_id5] => 5000
)
I am trying to insert each array as a record in a "user" table. I can do this fine as long as the items are static in number. I am stumbling on how to insert a 'n' number of roles (along with their id's). In this example, user 1 has 4 roles while user 2 has 5 roles. I am at a loss for how to test for the presence of one of these "role" element types within the existing foreach and process them distinctly from the other elements.
Normalising the data into two tables would be nice (user & roles) but I am at a loss for how to extract the roles info while populating the 'id' from the array as the foreign key to be used in a 'roles' table. I'd settle for serializing each user's roles into one string and storing it in the "user" table in a "roles" field. In either case, I am not clear how to grab the roles info into a separate array for processing differently from the other elements.
At the moment, I am limited to looping through the array and executing static INSERT statements. Obviously, this doesn't work with a dynamic set of elements to insert. My current loop minus role handling is:
foreach ($arr AS $item) {
$sql = "INSERT INTO user (id, organizationid, firstname, lastname, phone, email) VALUES ('$item[id]', '$item[organization_id]', '$item[firstname]', '$item[lastname]', '$item[phone]', '$item[email]')";
mysql_query($sql) OR die(mysql_error());
}
Conceptually, I think I need to create a new array inside the foreach that contains elements for 'id,' 'role_name,' and 'role_id.' I could then use this created array to populate a 'roles' table with foreign key, 'id,' to the user table. Is it a nested foreach that I need to implement??? If so, I am unclear about how to test for 'n' role elements ...
I think I have all the blocks to build what I need but I'm not sure how to put them together. Trial and error continues for now! Thanks for reading my long post.