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]; Link to comment https://forums.phpfreaks.com/topic/140426-solved-post-issue/ 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. Link to comment https://forums.phpfreaks.com/topic/140426-solved-post-issue/#findComment-734956 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 Link to comment https://forums.phpfreaks.com/topic/140426-solved-post-issue/#findComment-734959 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. =) Link to comment https://forums.phpfreaks.com/topic/140426-solved-post-issue/#findComment-734961 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...] Link to comment https://forums.phpfreaks.com/topic/140426-solved-post-issue/#findComment-734963 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.