gish Posted January 11, 2009 Share Posted January 11, 2009 hi phpfreaks I am creating a questionnaire for my website but I am having trouble the script. Each question has three options that will be posted 1,2,3 1 meaning yes, 2 meaning no, and 3 unsure. When it gets submitted to the next page I have to sort the amount of 1, 2 and 3's. Is there and easier way other than using the if statement for every post variable or can I use a loop statement? below is what I have so far. what I want to know is there a way to get the if ($_POST[business] == 1){ $Yes = $Yes + 1; } elseif ($_POST[business] == 2){ $No = $No + 1; }else{ $Unsure = $Unsure + 1; } echo $_POST[business]; echo $_POST[performance]; echo $_POST[budget]; echo $_POST[structure]; echo $_POST[goals]; echo $_POST[customer]; Quote Link to comment Share on other sites More sharing options...
gish Posted January 11, 2009 Author Share Posted January 11, 2009 Ok I have been doing some thinking. I am think what I am asking is there a way to count the Post variable array then put it into a loop so i can extract the numbers 1,2,3. Quote Link to comment Share on other sites More sharing options...
chronister Posted January 11, 2009 Share Posted January 11, 2009 <?php $Yes = 0; $No = 0; $Unsure = 0; $fields = array('business','performance','budget','structure','goals','customer'); foreach($fields as $k) { switch($_POST[$k]) { case 1: $Yes++; break; case 2: $No++; break; case 3: $Unsure++; break; } } ?> Something like this make it a little shorter for ya?? Nate Quote Link to comment Share on other sites More sharing options...
gish Posted January 12, 2009 Author Share Posted January 12, 2009 Thanks I never thought of putting the array into a variable (sillly me, I should know that ) that was what I was missing. =) Quote Link to comment Share on other sites More sharing options...
DarkSuperHero Posted January 12, 2009 Share Posted January 12, 2009 yeah, if your checkboxes/radio boxes all have the same name, add [] to the end of the name such as <input type="radio" name="answer" value="yes"> <input type="radio" name="answer" value="no"> <input type="radio" name="answer" value="unsure"> the $_POST['answer'] or $_GET['answer'] variable would be passed to your script...and you can check to see what that value equals... edit: [mis-read...] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.