Page 1 of 1

Trying to get a classification form to work

Posted: Thu Jun 22, 2006 11:12 am
by noclss2000
I'm fairly new to PHP and so I'm not completely fluent in the language. What I'm trying to do is this:

I have a classification form which basically is setup that each checkbox is worth a value, normally between 1 to 3. When the user checks the boxes that apply to them and clicks "Submit" I want the page to add up the total value between the boxes that were selected and echo the classification that applies to that value.
IE: if value is between 0-10 echo "you are in class 1"
if value is between 11-20 echo "you are in class 2"
etc.

Posted: Thu Jun 22, 2006 11:24 am
by feyd
what are you having trouble with?

Posted: Thu Jun 22, 2006 12:41 pm
by noclss2000
I just have no idea how to go about doing it.

I thought that I could just do a list of if's but somebody else said something about using some type of array but as I said, I'm fairly new to PHP and that is over my head. The only things I've really done with PHP is doing management sections for websites to add/edit/delete stuff from a database via the management section of the website.

Posted: Thu Jun 22, 2006 1:03 pm
by Luke
This isn't tested

Code: Select all

if(isset($_POST['values']){ // Check to see if they submitted anything

    $total = array_sum($_POST['values']); // Sum up the total

    if($total > 20){
        // User is in class one
    }
    elseif($total >10){
        // User is in class two
    }
    elseif($total > 5){
        // User is in class three
    }
    else{
        // User is in class four
    }
}


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <input type="checkbox" name="values[]" value="1"> Some text
 <input type="checkbox" name="values[]" value="3"> Some more text
 <input type="checkbox" name="values[]" value="1"> Some more text
 <input type="checkbox" name="values[]" value="3"> Some more text
 <input type="checkbox" name="values[]" value="2"> Some more text
</form>
Also, you might want to add a check to make sure it is an array before array_sum() ing it.

Posted: Thu Jun 22, 2006 3:39 pm
by noclss2000
Thanks, that's pretty much what I was thinking it should look like but wasn't sure on the exact syntax.

Looks like that should work pretty good, thanks a bunch!

Posted: Thu Jun 22, 2006 10:42 pm
by noclss2000
The Ninja Space Goat wrote:This isn't tested

Code: Select all

if(isset($_POST['values']){ // Check to see if they submitted anything

    $total = array_sum($_POST['values']); // Sum up the total

    if($total > 20){
        // User is in class one
    }
    elseif($total >10){
        // User is in class two
    }
    elseif($total > 5){
        // User is in class three
    }
    else{
        // User is in class four
    }
}


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <input type="checkbox" name="values[]" value="1"> Some text
 <input type="checkbox" name="values[]" value="3"> Some more text
 <input type="checkbox" name="values[]" value="1"> Some more text
 <input type="checkbox" name="values[]" value="3"> Some more text
 <input type="checkbox" name="values[]" value="2"> Some more text
</form>
Also, you might want to add a check to make sure it is an array before array_sum() ing it.
yeah, this is blowing up like bombs over Baghdad on me.
It doesn't like the "{" of the beginning if

Posted: Thu Jun 22, 2006 10:59 pm
by noclss2000
I got it working, you were missing another ")" after the isset and that's what was blowing it up. I figured out the rest.

Posted: Thu Jun 22, 2006 11:35 pm
by Luke
Awesome