Tallying up form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

Tallying up form

Post by gum1982 »

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?
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Tallying up form

Post by spider.nick »

I am sorry, but your code is not even close. Have you picked up a book? Have you read through any tutorials? We are all here to help. As a matter of fact, forums are a good reason that I am as successful as I am, today.

A small, two or three line example? Sure. However, you cannot expect anyone to write code for you; we all have other things to be doing.

Anyway, read up, re-code, and then let us know how we can help.

Nick
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Tallying up form

Post by jackpf »

You could call all inputs in the same group the same name, with [] on the end.

That will cause php to put the posted values into an array.

You can then use count() to see which one was chosen most.

That's how I'd do it anyway....
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

Re: Tallying up form

Post by gum1982 »

Thanks for the response it helped.

And can i just say to nicks comment have i picked up a book have i read tutorials yes i have.

I am new to PHP and i am still learning, and your right my code probably isnt close hence why i posted on a forum, sorry but have i completely missed the point of a forum i thought they were used for help and learning.

Some people learn better from books and tutorials, some learn better from examples and best practice,

I could probably just download a working script off the net but i actually want to figure out how to do it myself.

I am a flat out newbie, give me a chance for god sake.

Anyways this is where i got to with my code.

<?php
$a = "a";
$b = "b";
$c = "c";
?>


<form method="POST" action="">

<h2>How healthy is your business ?</h2>
<p class="head">How often do you launch a new product or service?</p>
<input type="radio" name="group1" id="1a" value="<?php echo $a; ?>" />A) Every year we launch new products – most of our growth comes from these. <br/>
<input type="radio" name="group1" id="1b" value="<?php echo $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="<?php echo $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="<?php echo $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="<?php echo $b; ?>" />B) I’m looking at collaborating with other companies to get into new markets. <br/>
<input type="radio" name="group2" id="2c" value="<?php echo $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="<?php echo $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="<?php echo $b; ?>" />B) We follow-up every quotation to find out who won the business, <br/>
<input type="radio" name="group3" id="3c" value="<?php echo $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
$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>";

if ( $most_chosen == $a )
{
echo "<p>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. You welcome suggestions for improvement and want to know about best practice in marketing, operations and finance. What else could you be doing to make your organisation even better? Stay on your toes and don`t get complacent, the world has changed beyond recognition over the past year. Talk to your peers and if you want some free unbiased advice on any improvements you are thinking of making, contact us.</p>";
}
elseif ( $most_chosen == $b )
{
echo "<p>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. Take carefull note of any C`s you may have in the assessment and by making a few improvements to marketing, operations and how people are managed, you could improve your profitability and make life a little easier for yourself. Our advice is free, so your next move could be to give us a call and find out the best way for you to move forward. </p>";
}
else
{
echo "<p>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. If you want a better analysis of the issues facing your business before you take action, you may wish to use our in-depth online Health Check, which will pinpoint the specifics that are holding your company back. At WBB we look at marketing, operations, strategy and finance and make sure everyone, managers and staff, are pulling together in the same direction to enable your business to achieve your ambitions. We know it can be tough out there so we are happy to give you some of our free professional advice.</p>";
}

?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Tallying up form

Post by jackpf »

Ok...having a go myself, your problems seems more confusing than I initially thought :P

Anyway, rather than explaining something which is pretty complicated, I thought I'd give you an example.

Code: Select all

<?php
    if(count($_POST) == 0)
    {
        echo '<form method="post">
            section 1:
            1:<input type="radio" name="g1" value="1" />
            2:<input type="radio" name="g1" value="2" />
            3:<input type="radio" name="g1" value="3" />
            <br /><br />
            section2:
            1:<input type="radio" name="g2" value="1" />
            2:<input type="radio" name="g2" value="2" />
            3:<input type="radio" name="g2" value="3" />
            <br /><br />
            <input type="submit" />
        </form>';
    }
    else
    {
        $groups = array(1 => 0, 2 => 0, 3 => 0);
        
        for($i = 1; isset($_POST['g'.$i]); $i++)
        {
            $groups[$_POST['g'.$i]] += 1;
        }
        
        print_r($groups);
    }
?>
That's how I decided to do it anyway.

From this, you can see what has been chosen, 1, 2, and 3. You can then check which one has been chosen the most.

Hope this helps.
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

Re: Tallying up form

Post by gum1982 »

Brilliant thanks for the reply i will give this a go now.
Post Reply