Page 1 of 1

How will I do this? Help someone.

Posted: Mon Dec 28, 2009 7:19 am
by kingdm
Good day..

I have my project in here which requires an automation of an application form. Part of it is it ask for the member if he/she has a dependent (son/daughter). If yes it ask for its name, gender, age and date of birth.

My problem is what will be my algorithm here in regards on the database of the dependent. I can't create one table for dependents since not all members have one dependent. What if he/she has 2 or more dependents.

Can someone help me in regards with this. Huge Thanks.

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 7:58 am
by kingdm
need help please anyone :(

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 8:00 am
by omniuni
Don't worry, it's simpler than you think. You don't need to split the data at all. Let's call these "people"

Each one of the members of table "people" will have a list of attributes, some of which apply only to those with dependents. You will, of course, want a column marked "dependents" with a boolean value (yes or no). Then, you would have columns for the POSSIBLE data. Quite simply, if they don't have a dependent, then you don't record the information.

I would recommend you use a bit of JavaScript in the form, so you have two radio buttons (yes and no) and if they click the "yes" button, it reveals part of the form that will ask them for the information, otherwise, it will simply hide the extra fields, and they are never recorded. If you want to retrieve the data later, you just make a database call to filter out those with dependents or without.

Oh, and by the way, we'll get around to helping you as soon as we can, it just sometimes takes us a bit of time. Before you bump your post, click "View new Posts" and see if it's on the first page, if it is, leave it be for a while.

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 8:51 am
by kingdm
omniuni wrote:Don't worry, it's simpler than you think. You don't need to split the data at all. Let's call these "people"

Each one of the members of table "people" will have a list of attributes, some of which apply only to those with dependents. You will, of course, want a column marked "dependents" with a boolean value (yes or no). Then, you would have columns for the POSSIBLE data. Quite simply, if they don't have a dependent, then you don't record the information.

I would recommend you use a bit of JavaScript in the form, so you have two radio buttons (yes and no) and if they click the "yes" button, it reveals part of the form that will ask them for the information, otherwise, it will simply hide the extra fields, and they are never recorded. If you want to retrieve the data later, you just make a database call to filter out those with dependents or without.

Oh, and by the way, we'll get around to helping you as soon as we can, it just sometimes takes us a bit of time. Before you bump your post, click "View new Posts" and see if it's on the first page, if it is, leave it be for a while.
Very well explained omniuni. I did think of this as an alternative to my problem but what limits me is my capability in regards with javascript. I might say I'm quite dumb when it comes to Javascript that's why I stick to PHP and mySQL. But well, as you've explained it did me a quite catch on it. Would you please assist me in regards with this?

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 10:41 am
by manohoo
The answer is in the table design. Try to alter your table to something like this:

TABLE: members

ID FIRST LAST DEPENDENT
01 JOHN DOE null
02 JANE DOE 01
03 JAMES DOE 01
04 DAVID SMITH null

In the table above there are two members (01 and 04), and two dependents (02 and 03, both of them are dependents of 01). The field DEPENDENT is your key, if the value is NULL, then you know that the ID is not a dependent, just a full member. If DEPENDENT has a value, then the record is dependent on the ID specified in that field.

If what you want to do is identify all full members, then the query would be:
SELECT * FROM members WHERE DEPENDENT is null;

If what you want to do is identify all dependents, then the query would be:
SELECT * FROM members WHERE DEPENDENT is not null;

... and so forth

Note that you would have to modify the form. What I would do is have a form to ask the first time member to enter number of dependents. If there were any dependents then the user would be presented with a new form, with as many entries as dependents were specified in the first form. Makes sense?

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 10:54 am
by omniuni
No problem. Check this out:

Code: Select all

<form>
<input type="radio" name="dependendents" value="d_no" checked="checked" onClick="document.getElementById('dependent_info').style.display='none'" />No<br />
<input type="radio" name="dependendents" value="d_yes" onClick="document.getElementById('dependent_info').style.display='inline'"/>Yes
 
<div id="dependent_info" style="display: none;">
<br/>Who? <input type="text" name="who"></input>
</div>
 
<br/><br/>
<input type="submit" />
</form>
Think you can go from there?

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 11:17 am
by kingdm
manohoo wrote:The answer is in the table design. Try to alter your table to something like this:

TABLE: members

ID FIRST LAST DEPENDENT
01 JOHN DOE null
02 JANE DOE 01
03 JAMES DOE 01
04 DAVID SMITH null

In the table above there are two members (01 and 04), and two dependents (02 and 03, both of them are dependents of 01). The field DEPENDENT is your key, if the value is NULL, then you know that the ID is not a dependent, just a full member. If DEPENDENT has a value, then the record is dependent on the ID specified in that field.

If what you want to do is identify all full members, then the query would be:
SELECT * FROM members WHERE DEPENDENT is null;

If what you want to do is identify all dependents, then the query would be:
SELECT * FROM members WHERE DEPENDENT is not null;

... and so forth

Note that you would have to modify the form. What I would do is have a form to ask the first time member to enter number of dependents. If there were any dependents then the user would be presented with a new form, with as many entries as dependents were specified in the first form. Makes sense?
Thanks for your input buddy. After reading your explanation, it broadens my understanding regarding this dependent key, which for me is a new learning, thanks for suggesting that asking the new member to ask for the number of dependents. I'll try to tidy up the code I'll post back if I found some errors. Thanks once again.
omniuni wrote:No problem. Check this out:

Code: Select all

<form>
<input type="radio" name="dependendents" value="d_no" checked="checked" onClick="document.getElementById('dependent_info').style.display='none'" />No<br />
<input type="radio" name="dependendents" value="d_yes" onClick="document.getElementById('dependent_info').style.display='inline'"/>Yes
&nbsp;
<div id="dependent_info" style="display: none;">
<br/>Who? <input type="text" name="who"></input>
</div>
&nbsp;
<br/><br/>
<input type="submit" />
</form>
Think you can go from there?
I understand the code you post buddy, but it seems that onClick event seems to confuse me. What will be inside the script in here if this would be my form? Thanks for keeping me assisted.

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 2:28 pm
by omniuni
Take a look at the code in your web browser. What it does is that by default, the "no" button is checked, and no extra input is displayed. When the user checks "yes" it simply displays the contents of the <div> element, so that they have the extra fields to fill in.

Re: How will I do this? Help someone.

Posted: Mon Dec 28, 2009 8:29 pm
by kingdm
omniuni wrote:Take a look at the code in your web browser. What it does is that by default, the "no" button is checked, and no extra input is displayed. When the user checks "yes" it simply displays the contents of the <div> element, so that they have the extra fields to fill in.
I run and tested the code your posted. Thanks for that, but I do have a question how will I add more fields whenever the yes is click?

Re: How will I do this? Help someone.

Posted: Tue Dec 29, 2009 1:03 am
by indian98476
you can type in the dependents in the textfield one after the othe with comma separated...then you will have to retrieve the values from text field using a delimiter function....

Re: How will I do this? Help someone.

Posted: Tue Dec 29, 2009 1:41 am
by omniuni
Inside of the two <div> tags, you can add any form fields you want. The more inputs you put in there, the more will appear when you click on "yes". All the Javascript does is show and hide whatever is in the <div>.

Re: How will I do this? Help someone.

Posted: Tue Dec 29, 2009 2:44 am
by kingdm

Code: Select all

<form>
<input type="radio" name="dependendents" value="d_no" checked="checked" onClick="document.getElementById('dependent_info').style.display='none'" />No<br />
<input type="radio" name="dependendents" value="d_yes" onClick="document.getElementById('dependent_info').style.display='inline'"/>Yes
&nbsp;
<div id="dependent_info" style="display: none;">
<br/>
Name of Dependent: <input type="text" name="dependent1"></input>
<br />
Birthday: <input type="text" name="dependent1"></input>
</div>
&nbsp;
<br/><br/>
<input type="submit" />
</form>
I revised the code a little. Now I see, what does the javascript does. Can I ask is it possible to have here a loop which will ask if how many dependents you have and then that will be the indicator on how many fields will be created?

Re: How will I do this? Help someone.

Posted: Tue Dec 29, 2009 3:53 am
by omniuni
Probably the easiest way is to have a button that will add a text input when clicked. This can be done with JavaScript, but I'm afraid JS is not really my strong suit, so you would probably do better to post a new topic for that over in the Client-Side topic.

Re: How will I do this? Help someone.

Posted: Tue Dec 29, 2009 4:49 am
by kingdm
omniuni wrote:Probably the easiest way is to have a button that will add a text input when clicked. This can be done with JavaScript, but I'm afraid JS is not really my strong suit, so you would probably do better to post a new topic for that over in the Client-Side topic.
Thanks omniuni. I'll post there as you suggested.