Page 1 of 1
php-mysql dynamically storing with different values
Posted: Mon Apr 20, 2009 5:30 am
by gwolff2005
Hi guys I need your help!
I have a site where the user registers and then logs on. The mysql database has the following fields:
id, username, password, email, firstname, name, resultspassion, resultsmotivation, resultspassionorientation
when the user logs on he is offered to do a psychological test with 92 questions. Every question can be answered by clicking a radiogroupbutton, which value is between 0 and 5.
Every value of the answer needs to be saved either in "resultspassion", "resultsmotivation" or "resultspassionorientation".
As an example:
Code: Select all
<td width="26">1</td>
<td width="411"><strong>Do you like talking to people?</strong><br />
</b></td>
<td width="230">
<div align="left">
<input name="resultspassion1" type="radio" value="0" />
0
<input name="resultspassion1" type="radio" value="1" />
1
<input name="resultspassion1" type="radio" value="2" />
2
<input name="resultspassion1" type="radio" value="3" />
3
<input name="resultspassion1" type="radio" value="4" />
4
<input name="resultspassion1" type="radio" value="5" />
5</div></td>
</tr>
<tr>
<td>2</td>
<td>Is it important for you what other people think about you?</b></td>
<td><p>
<label></label>
<label></label>
<input name="resultsmotivation2" type="radio" value="0" />
0
<input name="resultsmotivation2" type="radio" value="1" />
1
<input name="resultsmotivation2" type="radio" value="2" />
2
<input name="resultsmotivation2" type="radio" value="3" />
3
<input name="resultsmotivation2" type="radio" value="4" />
4
<input name="resultsmotivation2" type="radio" value="5" />
5<br />
<br />
</p></td>
</tr>
<tr>
<td>3</td>
<td class="style1">How high is your level of excitement right now? </td>
<td><p>
<label></label>
<input name="resultspassionorientation3" type="radio" value="0" />
0
<input name="resultspassionorientation3" type="radio" value="1" />
1
<input name="resultspassionorientation3" type="radio" value="2" />
2
<input name="resultspassionorientation3" type="radio" value="3" />
3
<input name="resultspassionorientation3" type="radio" value="4" />
4
<input name="resultspassionorientation3" type="radio" value="5" />
5<br />
</p></td>
The test goes over three pages. My trouble now is. At the end of the page I have a go on button
Code: Select all
<input name="Submit" type="submit" class="style1" value="go on" />
What can I do that
1. the answers are stored dynamically?
2. the values of teh answers are adding each time? (e.g. when I have on one page 10 questions or passion and he has at the end a value of 92 that the script counts all his values for passion together and then puts it in the table or puts each value of each answer in the table and adds them....
Please help!! Thanks!
Re: php-mysql dynamically storing with different values
Posted: Mon Apr 20, 2009 10:31 am
by MasterBeta
This should do it.
Code: Select all
<?php
if($_POST['submit']){ // check if form is submitted
$total = 0;
foreach($_POST as $score){
$total = $total + $score;
}
print "You scored $total";
}else{ // form not submitted
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table><tr>
<td>1</td>
<td>Do you like talking to people?</td>
<td>
<input name="resultspassion1" type="radio" value="0" />0
<input name="resultspassion1" type="radio" value="1" />1
<input name="resultspassion1" type="radio" value="2" />2
<input name="resultspassion1" type="radio" value="3" />3
<input name="resultspassion1" type="radio" value="4" />4
<input name="resultspassion1" type="radio" value="5" /> 5</td>
</tr>
<tr>
<td>2</td>
<td>Is it important for you what other people think about you?</td>
<td>
<input name="resultsmotivation2" type="radio" value="0" />0
<input name="resultsmotivation2" type="radio" value="1" />1
<input name="resultsmotivation2" type="radio" value="2" />2
<input name="resultsmotivation2" type="radio" value="3" />3
<input name="resultsmotivation2" type="radio" value="4" />4
<input name="resultsmotivation2" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>3</td>
<td>How high is your level of excitement right now? </td>
<td>
<input name="resultspassionorientation3" type="radio" value="0" />0
<input name="resultspassionorientation3" type="radio" value="1" />1
<input name="resultspassionorientation3" type="radio" value="2" />2
<input name="resultspassionorientation3" type="radio" value="3" />3
<input name="resultspassionorientation3" type="radio" value="4" />4
<input name="resultspassionorientation3" type="radio" value="5" />5
</td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
}
?>
Re: php-mysql dynamically storing with different values
Posted: Mon Apr 20, 2009 3:00 pm
by gwolff2005
Hi Masterbeta,
thanks for your help. But as I see (or I just did not get it) teh code adds everything together.... But what I need is is e.g. the answer of question one needs to be added to the existing value of the mysql table to the current user in the column passion. the seconde question in another row (still the current user). but they need to add just in their cell not All OF THEM together....
Re: php-mysql dynamically storing with different values
Posted: Mon Apr 20, 2009 4:25 pm
by MasterBeta
I thought you were trying to add all of them together.
So is there a DB field for each "question" OR for each "question type" like (passion, motivation, etc).
If you could explain your DB structure I could probably help.
I understand now that you want to add a value to an existing one in the DB, but I still need more info.
Re: php-mysql dynamically storing with different values
Posted: Tue Apr 21, 2009 12:39 am
by gwolff2005
Hi Masterbeta,
The mysql database has the following fields:
id, username, password, email, firstname, name, resultspassion, resultsmotivation, resultspassionorientation
So that means that e.g. all the questions which belong to passion need to go into that field but need to be added...
Thanks so much!!
Re: php-mysql dynamically storing with different values
Posted: Tue Apr 21, 2009 2:37 am
by MasterBeta
Okay, I think I understand now. Now that I read your initial post all the info was there I just wasn't connecting the dots. Anyways, this could be simplified if each question wasn't intermixed but I assume from your example they will be. Here's an example.
Code: Select all
<?php
if($_POST['submit']){ // check if form is submitted
$passionScore = 0;
foreach($_POST[passion] as $score){
$passionScore = $passionScore + $score;
}
$motivationScore = 0;
foreach($_POST[motivation] as $score){
$motivationScore = $motivationScore + $score;
}
$orientationScore = 0;
foreach($_POST[orientation] as $score){
$orientationScore = $orientationScore + $score;
}
print "Your passion score is $passionScore<br />";
print "Your motivation score is $motivationScore<br />";
print "Your orientation score is $orientationScore<br />";
}else{ // form not submitted
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table><tr>
<td>1</td>
<td>Passion Question 1</td>
<td>
<input name="passion[1]" type="radio" value="0" />0
<input name="passion[1]" type="radio" value="1" />1
<input name="passion[1]" type="radio" value="2" />2
<input name="passion[1]" type="radio" value="3" />3
<input name="passion[1]" type="radio" value="4" />4
<input name="passion[1]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>2</td>
<td>Motivation Question 1</td>
<td>
<input name="motivation[1]" type="radio" value="0" />0
<input name="motivation[1]" type="radio" value="1" />1
<input name="motivation[1]" type="radio" value="2" />2
<input name="motivation[1]" type="radio" value="3" />3
<input name="motivation[1]" type="radio" value="4" />4
<input name="motivation[1]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>3</td>
<td>Orientation Question 1</td>
<td>
<input name="orientation[1]" type="radio" value="0" />0
<input name="orientation[1]" type="radio" value="1" />1
<input name="orientation[1]" type="radio" value="2" />2
<input name="orientation[1]" type="radio" value="3" />3
<input name="orientation[1]" type="radio" value="4" />4
<input name="orientation[1]" type="radio" value="5" />5
</td>
</tr><tr>
<td>4</td>
<td>Passion Question 2</td>
<td>
<input name="passion[2]" type="radio" value="0" />0
<input name="passion[2]" type="radio" value="1" />1
<input name="passion[2]" type="radio" value="2" />2
<input name="passion[2]" type="radio" value="3" />3
<input name="passion[2]" type="radio" value="4" />4
<input name="passion[2]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>5</td>
<td>Motivation Question 2</td>
<td>
<input name="motivation[2]" type="radio" value="0" />0
<input name="motivation[2]" type="radio" value="1" />1
<input name="motivation[2]" type="radio" value="2" />2
<input name="motivation[2]" type="radio" value="3" />3
<input name="motivation[2]" type="radio" value="4" />4
<input name="motivation[2]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>6</td>
<td>Orientation Question 2</td>
<td>
<input name="orientation[2]" type="radio" value="0" />0
<input name="orientation[2]" type="radio" value="1" />1
<input name="orientation[2]" type="radio" value="2" />2
<input name="orientation[2]" type="radio" value="3" />3
<input name="orientation[2]" type="radio" value="4" />4
<input name="orientation[2]" type="radio" value="5" />5
</td>
</tr><tr>
<td>7</td>
<td>Passion Question 3</td>
<td>
<input name="passion[3]" type="radio" value="0" />0
<input name="passion[3]" type="radio" value="1" />1
<input name="passion[3]" type="radio" value="2" />2
<input name="passion[3]" type="radio" value="3" />3
<input name="passion[3]" type="radio" value="4" />4
<input name="passion[3]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>8</td>
<td>Motivation Question 3</td>
<td>
<input name="motivation[3]" type="radio" value="0" />0
<input name="motivation[3]" type="radio" value="1" />1
<input name="motivation[3]" type="radio" value="2" />2
<input name="motivation[3]" type="radio" value="3" />3
<input name="motivation[3]" type="radio" value="4" />4
<input name="motivation[3]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>9</td>
<td>Orientation Question 3</td>
<td>
<input name="orientation[3]" type="radio" value="0" />0
<input name="orientation[3]" type="radio" value="1" />1
<input name="orientation[3]" type="radio" value="2" />2
<input name="orientation[3]" type="radio" value="3" />3
<input name="orientation[3]" type="radio" value="4" />4
<input name="orientation[3]" type="radio" value="5" />5
</td>
</tr><tr>
<td>10</td>
<td>Passion Question 4</td>
<td>
<input name="passion[4]" type="radio" value="0" />0
<input name="passion[4]" type="radio" value="1" />1
<input name="passion[4]" type="radio" value="2" />2
<input name="passion[4]" type="radio" value="3" />3
<input name="passion[4]" type="radio" value="4" />4
<input name="passion[4]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>11</td>
<td>Motivation Question 4</td>
<td>
<input name="motivation[4]" type="radio" value="0" />0
<input name="motivation[4]" type="radio" value="1" />1
<input name="motivation[4]" type="radio" value="2" />2
<input name="motivation[4]" type="radio" value="3" />3
<input name="motivation[4]" type="radio" value="4" />4
<input name="motivation[4]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td>12</td>
<td>Orientation Question 4</td>
<td>
<input name="orientation[4]" type="radio" value="0" />0
<input name="orientation[4]" type="radio" value="1" />1
<input name="orientation[4]" type="radio" value="2" />2
<input name="orientation[4]" type="radio" value="3" />3
<input name="orientation[4]" type="radio" value="4" />4
<input name="orientation[4]" type="radio" value="5" />5
</td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
}
?>
For each radio button in each group you will need to sign an array value. You could have hundreds if you want. Ex. passion[199], passion[200]... etc. Just make sure they belong to the right group and you're set.
Also this script does not have any validation to it so if the user left a field blank it would not be added. We could add some validation but wasn't sure I was on the right track anyway.