|
Yes, which is what I had except there was two of them... not thirty. You would have to get the results of each question array. I think instead of something like q1[] and q2[] you can probably do something like q[1][], q[2][], etc... So that you have a 2-dimensional array containing every choice for every question. This way you can easily iterate over each question, adding up the points if they selected 'yes'.
So i wrote a quick script, this script will work if you name each radio button a name of a question, as stated above eg radio name="q1[yes]" value="1", if you do not decide to do this the code will need to be modified.
Also note the code is not perfect i literally wrote it in under 5 minutes, so there are obviously better methods to do this, and if you were more clear on what you are in need of i possibly could have made it more effective.Code:$y = 0; // variable containing yes to questions $n = 0; // variable containing no to questions foreach($_POST as $u => $i) { if(strpos($u, 'q') === 0) // checks all inputs sent through post in which their name starts with q { $y+= $i; // Adds 1 to yes variable if the radio is equal to yes $n+= $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no } }
Unsupported operand types - error.. I'm terrible at errors so I have no idea how to fix this.
Code:if (isset($_POST['submitti'])) { $y = 0; // variable containing yes to questions $n = 0; // variable containing no to questions foreach($_POST as $u => $i) { if(strpos($u, 'q') === 0) { $y+= $i; // it's pointing at this line. $n+= $i == 0 ? 1 : 0; } }
Which line was the error on?
try replacing the corresponding line of code with this
Code:$n+= $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to noCode:if($i == 0) $n += 1; // Adds 1 to no variable if the radio is equal to no
Try adding a space after it so it would be like $y += $i; or try this alternate code which should definitely work
Code:$y == $y + $i; // Adds 1 to yes variable if the radio is equal to yes $n == $n + $i == 0 ? 1 : 0; // Adds 1 to no variable if the radio is equal to no
Haha, can't believe i made that mistake, sorry, the 2 equals should only be one, so just backspace one on each line herp.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |