[K12OSN] PHP and variables

Doug Simpson simpsond at leopards.k12.ar.us
Tue Sep 9 18:24:16 UTC 2008


Yes. They will increment each time the page is submitted. It posts to itself so it automatically reloads every time you hit the submit button.
The form collects the data and passes it to the php portion with the form action.
The form action posts back to the same script. This causes the page to just reload after you hit submit and it is supposed to tabulate each choice and give a running total of each choice.

Don't waste too much of your time on it. . . it is just a for-fun thing for one of my teachers.

Thanks for your insight anyway! Will keep plugging away. So far, I have received several examples of the string conversion and each works about the same, but none of then actually add each trip through the script. . .

Doug

Doug Simpson
Technology Specialist
De Queen Public Schools
De Queen, AR
simpsond at leopards.k12.ar.us
"A Dollar Saved is a Dollar Earned"


>>> Henry Hartley <henryhartley at westat.com> 9/9/2008 12:00 PM >>>
Doug Simpson wrote:

>> I can't get this to add them up.

Are you talking about them adding to a running total each time the page is submitted?  If so, you'll need something other than regular variables.  If you want each submissions from a user to be added to previous submissions, then you need to replace your $chosenatotal, etc. variables with session variables.

I still would use a hash (that's a associative array when it's at home) rather than all that switch code.

$_SESSION["chosentotal"][$choice]++ ;

will do the same thing in one line as your case statement does and if you add a third choice you won't need to add any code.

Then you can print them all with this:

ksort($_SESSION["chosentotal"]) ; // optionally sort them first
foreach ( array_keys($_SESSION["chosentotal"]) as $choice => $count ) {
        print "$choice = $count<br>\n" ;
}

--
Henry

_______________________________________________
K12OSN mailing list
K12OSN at redhat.com 
https://www.redhat.com/mailman/listinfo/k12osn 
For more info see <http://www.k12os.org>





More information about the K12OSN mailing list