Trying to get a classification form to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noclss2000
Forum Newbie
Posts: 7
Joined: Thu Jun 22, 2006 11:11 am

Trying to get a classification form to work

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what are you having trouble with?
noclss2000
Forum Newbie
Posts: 7
Joined: Thu Jun 22, 2006 11:11 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
noclss2000
Forum Newbie
Posts: 7
Joined: Thu Jun 22, 2006 11:11 am

Post 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!
noclss2000
Forum Newbie
Posts: 7
Joined: Thu Jun 22, 2006 11:11 am

Post 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
noclss2000
Forum Newbie
Posts: 7
Joined: Thu Jun 22, 2006 11:11 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Awesome
Post Reply