gum1982 Posted July 29, 2009 Share Posted July 29, 2009 Hi im trying to setup a form, what i need it to do is at the end tally up all the choices off a, b or c for each question and then display. Your choose mostly: A Your choose mostly: B Your choose mostly: C This is where i am so far im new to php can someone help me please? <style type="text/css"> p.head { color:#F00; font-family:Arial, Helvetica, sans-serif; font-weight:700; } </style> <form method="POST" action=""> <p class="head">How often do you launch a new product or service?</p> <input type="radio" name="group1" id="1a" value="a" />A) Every year we launch new products – most of our growth comes from these. <br/> <input type="radio" name="group1" id="1b" value="b" />B) It’s been a long time, but customers are happy with the products we have in our range. <br/> <input type="radio" name="group1" id="1c" value="c" />C) We concentrate on keeping prices down, there’s too much competition out there.<br/> <p class="head">Are there any new markets you could sell to?</p> <input type="radio" name="group2" id="2a" value="a" />A) A. Yes, we’re discovering lots of new application for our products & services, especially export markets. <br/> <input type="radio" name="group2" id="2b" value="b" />B) B. I’m looking at collaborating with other companies to get into new markets. <br/> <input type="radio" name="group2" id="2c" value="c" />C) C. All my time is taken up defending our position and keeping the customers we have. </p> <p class="head">How do you win new customers?</p> <input type="radio" name="group3" id="3a" value="a" />A) A. Our products and services are unique – people tell us we are the number-one supplier. <br/> <input type="radio" name="group3" id="3b" value="b" />B) B. We follow-up every quotation to find out who won the business, <br/> <input type="radio" name="group3" id="3c" value="c" />C) C. We lost a big customer recently, and I need to work out how to replace those sales <br/> <p><input type="submit" name="submit"> </p> </form> <?php //The variables from the form in the previous page collected here: $rad=$_POST['group1']; $rad=$_POST['group2']; $rad=$_POST['group3']; //"select1" and "R1" are the names of form elements //the values of the form elements are given to these variables //and we show them below: echo "You chose mostly: ".$rad."<br>"; ?> Its only giving me the result of the lest group as i said im new to php do i need to run an array through the questions? Quote Link to comment https://forums.phpfreaks.com/topic/167951-radio-buttons-a-b-c-mostly-choosen/ Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 $chosen_most = array('a' => 0, 'b' => 0, 'c' => 0); if (!empty($_POST)) {//an answer has been posted switch ($answer) { case 'a': $chosen_most['a']++; break; case 'b': $chosen_most['b']++; break; case 'c': $chosen_most['c']++; break; } } arsort($chosen_most); echo 'You chose mostly: ', array_shift(array_keys($chosen_most)); Quote Link to comment https://forums.phpfreaks.com/topic/167951-radio-buttons-a-b-c-mostly-choosen/#findComment-885833 Share on other sites More sharing options...
patrickmvi Posted July 29, 2009 Share Posted July 29, 2009 You could do something like this: $most_chosen_arr = array(); for($i = 1; $i <= 3; $i++) { $most_chosen_arr[$_POST["group" . $i]]++; } arsort($most_chosen_arr); $most_chosen = key($most_chosen_arr); echo "You chose mostly: ".$most_chosen."<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/167951-radio-buttons-a-b-c-mostly-choosen/#findComment-885834 Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 You could do something like this: $most_chosen_arr[$_POST["group" . $i]]++; Will throw Notice: undefined index 'a'. $most_chosen_arr[$_POST["group" . $i]]++ will receive the value null on which you perform an integer operation (++) which results in 1. This fixes this problem: if (!isset($most_chosen_arr[$_POST['group' . $i]])) { $most_chosen_arr[$_POST['group' . $i]] = 1; } else { $most_chosen_arr[$_POST['group' . $i]]++; } Quote Link to comment https://forums.phpfreaks.com/topic/167951-radio-buttons-a-b-c-mostly-choosen/#findComment-885840 Share on other sites More sharing options...
gum1982 Posted July 29, 2009 Author Share Posted July 29, 2009 Hey guys thanks for the quick response amazing. don't suppose you could talk me through this is actually doing please, just for future reference. The only extra thing i need to include is when it says you have choosen mostly a i need to assign a paragraph of text to each letter. example : you have choosen mostly a : Mostly As: Congratulations - Your business is in good shape. Your people enjoy coming to work and are proud of the organisation. Their day-to-day activities are linked to your objectives. Managers are effective and people believe their contribution is valued. Competitors have a tough time keeping up with you. Can i assign this with a variable and the text reference the variable with an if else statement. if ( $a ) { Mostly As: Congratulations - Your business is in good shape. Your people enjoy coming to work and are proud of the organisation. Their day-to-day activities are linked to your objectives. Managers are effective and people believe their contribution is valued. Competitors have a tough time keeping up with you. } else ( $b ) { Mostly Bs: Well done – you passed the MOT. You have most areas under control and you understand the value that good operation in all departments can add to your business, however , but there are a couple of things that need your attention before they cause a problem. } else ( $c ) { Mostly Cs: There are quite a few issues you need to look at in order to pass an MOT. Fortunately there is plenty of support available, and some simple tools and techniques that could give you a few “early wins” to get the organisation back on track } i know this is really rough but is this they best way to do it or is a switch statement better. Sorry if im being dull still learning. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/167951-radio-buttons-a-b-c-mostly-choosen/#findComment-885953 Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 $most_chosen_texts = array('a' => 'Mostly A\'s: ..', 'b' => 'Mostly B\'s: ..', 'c' => 'Mostly C\'s: ..'); $most_chosen = key($chosen_most); print $most_chosen_texts[$most_chosen]; Quote Link to comment https://forums.phpfreaks.com/topic/167951-radio-buttons-a-b-c-mostly-choosen/#findComment-886163 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.