[K12OSN] PHP and variables
simpsond at leopards.k12.ar.us
Tue Sep 9 14:46:14 UTC 2008
I am trying to make a small form for elementary kids to choose from several items, like voting on a favorite book, for example.
Radio buttons are clicked and that sets a variable with a letter, "a" for the first choice and "b" for the second choice for example and set them in a variable called "$choice". Choice will contain either "a" or "b".
Now, do an if statement, like:
if $choice="a" then $chosena=1
if $choice="b" then $chosenb=1
now, I want to do something like:
$chosenatotal=$chosena+1 ($chosena should contain a numerical value, 1 or 0 depending on the choice made)
display the total for a
$chosenbtotal=$chosenb+1 ($chosenb should contain a numerical value, 1 or 0 depending on the choice made)
display the total for b
I know how to display the totals, too using the system function. I just need it to add the $chosenxtotal.
Each trip through the script will either increment $chosenatotal or $chosenbtotal depending on which choice was made.
I hope this helps you to understand. . . basically I just need to know how to convert a letter variable to a numeric variable. The radio button will only allow you to chose one or the other.
Don't waste too much time on it. This is just something I am playing with for a teacher here at the school.
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 8:06 AM >>>
Doug Simpson wrote:
>> I know how to get data in from a form, but I need to know how to
>> get a string converted from a letter variable to a number variable.
>> Say there are two choices and I am using radio buttons to select
>> which choice the viewer wants.
>> It stores the first choice as "a" and the second choice as "b".
>> This I know how to do.
>> So I would like to convert the a to 1 and the b to 1 and store
>> them each in another variable of their own. I know how to get
>> them into another variable, also.
>> Now, I want to add up the number of "a"s and the number of "b"s .
>> All I really need to know is how to convert a letter to a number
>> in PHP.
>> if choice=a then choice1num=1 <<<<this part
>> if choice=b then choice2num=1 <<<<this part
>> echo choice1num
>> echo choice2num
It isn't completely clear to me what you want to do.
If you have letters a, b, c, and possibly as far as z, then you can use ord() and subtract 60. Since ord("a") will return 61, you'll end up with a 1 for a, 2 for b, etc. Is that what you mean?
Or do you want to cound the number of occurrences of a, b, c, etc.? If so, then convert to a hash. Inside a loop that goes through all the selections made, have something like this:
When you're done, $count["a"] will be the number of a's, $count["b"] the number of b's, etc.
The last part of your question (the "All I really need to know") is the least clear part of your email. Perhaps you can explain what you're trying to do, rather than be quite so specific about how to do it.
K12OSN mailing list
K12OSN at redhat.com
For more info see <http://www.k12os.org>
More information about the K12OSN