[K12OSN] PHP and variables

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


I can't get this to add them up.

Here is my modification to make it display the results. . .

<?php
$choice = escapeshellcmd($choice);
import_request_variables("P")
switch ($choice) {
  case "a":
    $chosenatotal=$chosenatotal+1;
system("echo $chosenatotal is first");
  case "b":
    $chosenbtotal=$chosenbtotal+1;
}
system(echo"$chosenbtotal is second");
system("echo $chosenatotal, $chosenbtotal");

?>

It does echo the 1's, but it does not add them together to increment. Your example does give the 1 in place of the a or b in the appropriate variable, but it won't increment them each time.

Thanks again for your assistance!

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"


>>> "Rob Asher" <rasher at paragould.k12.ar.us> 9/9/2008 11:05 AM >>>
If you're just wanting a counter, something like this might work for you:

switch ($choice) {
    case "a":
        $chosenatotal=$chosenatotal+1;
        break;
    case "b":
         $chosenbtotal=$chosenbtotal+1;
         break;
}

HTH,
Rob


-------------------------------------
Rob Asher
Network Systems Technician
Paragould School District
(870)236-7744 Ext. 169


>>> "Doug Simpson" <simpsond at leopards.k12.ar.us> 9/9/2008 9:46 AM >>>
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.

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 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.
>>
>> Example:
>>
>> 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:

        $count[$choice]++ ;

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.

--
Henry

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


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

---------- 

This message has been scanned for viruses and
dangerous content by the Paragould School District
MailScanner, and is believed to be clean.


---------- 

This message has been scanned for viruses and
dangerous content by the Paragould School District
MailScanner, and is believed to be clean.


_______________________________________________
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