[K12OSN] PHP and variables

Henry Hartley henryhartley at westat.com
Tue Sep 9 17:00:53 UTC 2008

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" ;


More information about the K12OSN mailing list