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.
Trying to get a classification form to work
Moderator: General Moderators
-
noclss2000
- Forum Newbie
- Posts: 7
- Joined: Thu Jun 22, 2006 11:11 am
-
noclss2000
- Forum Newbie
- Posts: 7
- Joined: Thu Jun 22, 2006 11:11 am
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.
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.
This isn't tested
Also, you might want to add a check to make sure it is an array before array_sum() ing it.
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>-
noclss2000
- Forum Newbie
- Posts: 7
- Joined: Thu Jun 22, 2006 11:11 am
-
noclss2000
- Forum Newbie
- Posts: 7
- Joined: Thu Jun 22, 2006 11:11 am
yeah, this is blowing up like bombs over Baghdad on me.The Ninja Space Goat wrote:This isn't testedAlso, you might want to add a check to make sure it is an array before array_sum() ing it.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>
It doesn't like the "{" of the beginning if
-
noclss2000
- Forum Newbie
- Posts: 7
- Joined: Thu Jun 22, 2006 11:11 am