Retrieve a table's fields from a lookup table dynamically - php

I am making a mysql/php project and in one form, the specs require dynamic fields in a way that you have an initial selectbox with a value from a lookup table that describe job unions. Depending on the selected value, it will spawn (probably via reflection) different fields.
For example, I have 2 associations with ids 1 and 2. If the user selects union #1, then the fields would be first name, last name, phone, address. If the user selects union #2 then the fields would be mobile, email, im name, "enroll now?"(checkbox).
Now, this might occur often, because the total of unions are more than 10 and specs require it to be flexible.
What I thought is this:
form loads up the fields of the first lookup (jobunions)
user selects the job union, and the value of the selectbox is the name of another table, for example LK_TABLE_2
The reflection examines the LK_TABLE_2 fields and retrieves/renders the fields below the selectbox of step 1.
I need your opinion on whether this business logic is acceptable (is there a pattern I could use?), if not anything to suggest and if it's doable how to store the filled data into the user preferences table.
Any insight would do.
Update: here is a schema (pdf) of what I am trying to do
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B_vptVa0K8J2YjBjMGJmZDgtYzUxZi00ZTE5LTgxZjgtOTNlMjQ5OGM3ZTY1&hl=en_US

I am closing this question, because of the project that was terminated, and further more I don't work in the specific company anymore (over half a year ago).
We jumped to a more nosql solution before this happened and it was working fine... until termination.

Related

Database set up for multi-way relationships and form data collecting

I've posted a few questions on here and have gotten very great help and support. I'm still fairly new to programming and I'm putting together what I thought would be a simple website for the company I work at. I apologize in advance for my lengthy post/question, I just want to be thorough and clear in what I'm asking. My question is more of needing some help getting pointed in the right direction of how to get started and some best practices to be aware of. What I'm working on right now is to create a system where a user can submit a questionnaire/online form to inquire about a specific product (in this case it's a hard money loan product). The way I am planning on setting it up is to have a database with multiple tables (users, user_info, loan_app, property) and connect these together by referencing each other. I've read about table joins and I understand them conceptually but I have no idea how to implement in practice. I've had a hard time finding actual examples.
Specifically, this is what I am doing and how I am thinking it should work (correct me if I'm wrong or if there's a better way to do it):
1- the user (aka the borrower) signs in to the website. The user log in system references the user table where things like first name, last name, user name, password and user ID are stored. I have included an "active" column in this table so that when a user logs in the condition for them to get into the website is that the username and password match AND the user is activated. This way we can control on the back end certain user accounts access. I have this part working.
2- when the user registers, they only fill out the information that creates a new record in the "user" table. I have created a second table called "user_info" that will contain other data like home address, phone number email etc. But I need to be able to associate the correct record with right user. This is my first issue to wrap my head around. My thinking behind doing this instead of simply putting all this information in the user table is that for one, I might keep adding to that table and make it very big, and two for security reasons, I would like to keep the information separate. I don't know if this thought process has any merit to it though. Again, that's why I'm posting this here.
3- The user, once logged in, clicks on a button on their home screen/dashboard that will take them to the loan "pre-approval application" form, which is the questionnaire. On this form their basic information will be echoed/posted from the "user_info" table to pre-populate certain fields like first name, last name, email, phone number, address etc. So going back to #2 making sure I can associate the user with the correct record in the "user_info" table is critical. THEN, there are additional fields that the user has to fill out in order to submit the application/questionnaire. These form fields will create a new record in the "loan_app" table. This table will have a loanid column that is the primary key for that table, and an auto generated/randomized 6 or 7 digit loan number (loannum). The loanid will be a hidden value but the loan number will be like a reference number that is associated with the loan for the life of it and used for later accounting and recording purposes internally, whether or not it actually becomes a loan. The loanid, I'm assuming here, is the Foreign key in the "user" table and the userid is the Foreign key in the "loan_app" and "user_info" tables correct? If so, how do I incorporate being able to simultaneously associate all these records when the loan application/questionnaire is submitted? My thought would be write individual php scripts that does each of these things separately then have a "master" php that includes all of those individual ones that is placed as the form action associated with the submit button on the form.
Thanks for taking the time to read through this. I'd really appreciate any advice or reference material that I can read up on to learn more about this stuff. My job has a pretty crazy schedule and I travel a lot so I don't have the time to take actual classes to learn this stuff formally. I'm pretty much doing this as I go.
Also, I'm using MAMP with mysql, not sure if that helps any or not...
The user table's primary key userid can be the primary key of the user_info table as well, since each user will have only one user_info record, right? A foreign key constraint is good to ensure only valid userids get recorded in user_info.
The loan_app table can contain a denormalized relationship from loanid to userid so that each loan application is associated with a user. Again, use an FK constraint for integrity.
Don't include loanid in the user table - that would mean each user has a relationship to a single loan application. You already have the one-to-many relationship you need in the loan_app table.

Handling one-to-many references in MySQL

I didn't know how to give title to this but I have the following database:
accidentDetain(id, location, weather_conditions desc (and few more columns));
weatherConditions(id, title)
acc_weat_cond(id, wc_id, ad_id)
wc_id = weatherConditions ID, ad_id = accidentDetain ID
Now the situation is the user can store multiple weather conditions such as (rain, wet, snow Ice Fog etc)
Let's say user chooses 3 out of those 6 options and those will be stored in acc_weat_cond table, with accident Detail id and weather conditions id.
After saving, the user decided to change and to unchecked one of the option and then presses the save button. The issue is, there are 3 records already stored into acc_weat_cond table how would I would change and make them to two records.
Will I have to delete the first records from the database and then store again newly checked options? Or is there any easier way doing the above mentioned situation.
One last option is that I violate the role of database normalization and stored directly in the accidentDetails table and separate the values with a comma.
Feel free to ask if any more information is required...
I would have an <input type='hidden'name='checkedflds' value='1,2,3' />-field which contained the values that were checked before the user updated. Then after postback, you can compare the new list against this and will easily see what additions he made and which elements he removed...
I would call all of the options and compare to what is checked, and delete what you need. If you store it on the form, then there is a potential for out of date data.

What is the best way to handle large recursive queries in mysql?

Using PHP & Mysql-
I have a list of 120,000 employees. Each has a supervisor field with the supervisor employee number.
I am looking to build something that shows the employees in a tree like format. Given that if you click on anyone that you have an option to download all of the employees (with their info) that are under them.
So two questions - should I write my script to handle the query (which I have but is SLOW) or should create some sort of helper table/view? I am looking for best practice behind this.
Also I am sure this has been done a million times. Is there a good class that handles organization hierarchy?
The standard way of doing this is to use one table to store all of the employees, with a primary key field for the employee_id, and a field for supervisor_id which is a 'self join' - meaning that the value in this field points back to the employee id of this employee's supervisor. As far as displaying the employee tree - for relatively small trees, the entire tree structure can be sent to the client's browser when the page is created, and tree nodes can be displayed as the nodes are clicked from the stored data. But, for larger trees, it is better to fetch the data as needed, i.e. when the nodes are clicked. If you have 120,000 employees, then you might want to use the later approach.

PHP/MYSQL store variables in array or separate fields

Premature optimization is the root of all evil...but...
I am allowing users to input data within categories as in favorite players, favorite teams etc. They can then use these choices to filter results. I let them input lists separated by commas so after exploding the data I have it in an array. So how to store.
Method 1: I could create a table of users, one row per user, with the categories, as in players, teams as fields and save the choices of each users as an array in the respective field. (userid would link to basic users table.)
Method 2. Or I could create separate tables for each thing, players, teams, etc, and have a fixed number of fields say 10, break up the array into each individual value, store and place it in its own field. (Already have this code working.) (Again userid is primary key.)
The advantage of Method 1 is it's a bit simpler, one table, no limit on number of choices.
Method 2 seems a bit more robust. The data is more visible and possibly easier to get and retrieve--although maybe not.
Does anyone have experience with this sort of thing and could recommend one over another?
Thanks for any recommendations, suggestions!

Generic structuration of a web form and a MySQL's table for saving a array of undefined length

In a web form i will ask the user for their job experiences, this data will have no fixed lenght. I need to let the user insert all the items he needs, every item will content 3 fields; job title, description and year.
My firts problem is, how can i ask in the html form for the items? i mean, whats the best way to ask items with no fixed lenght using html/php (and maybe ajax)? I saw some sites that have a button (add one) when you hit it a new item slot is showed, but i have no idea of how to implement this, an example will be sufficient.
The second part is, how can i managed the data flow in post or get?, until now, i only use fixed fields, so i always know in my php script how many post or get vars i will get. How can i use multiple POST vars without knowing the amount of them?
And the last one (and the more important), how will be the best structure for my table in MySQL? If i get multiple items for a fixed table where i will have all my users, how can i resolve the multiple items issue? For example, if my table is:
User | password | job_experiences
admin | root | (this is just a cell, how can i save multiple items here?)
jonh | 1234 | (this is just a cell, how can i save multiple items here?)
Thanks for any help!!!!
Those are 3 questions, and it's best to post 3 questions, instead of discussing all of them. I will post the basics, and if you have specific questions, ask.
First, use button to add, and a JavaScript to clone an existing row (which can have more then one input field). For fieldnames use something like company_name[] - the [] is the important part, at this will send the field as an array. If you are editting profile, you can use company_name[$id] to preserve the mapping.
Second, in PHP you will receive this as $_POST['company_name'] which will be numeric array with all the company names. Or if you specify $id - with the corresponding keys. So, you have to loop trough all company_names, if there are other fields - you retrieve them the same way, using the current key. Example:
for (i =0; i<$_POST['comany_name'].length;i++) {
$company = $_POST['comany_name'][$i];
$start_year = $_POST['from'][$i];
...
}
Next, you need 1 table for the users (username, password), and another for job experiences (userid, company, description, from, to). This is called 1:M relation

Categories