noelm Posted January 19, 2011 Share Posted January 19, 2011 hi again guys, i was wondering if i could get some help. i have a form where the user has to enter his or her weight, and then the program will output their weight class. im having a few problems with this. im not sure how to tell php to check for the closest weight and give the answer. i'll give you a for instance: in the array i have ('featherweight' => 54) i need to tell php that everything below 53 is classed as featherweight then output "featherweight" $weight = $_GET['weight_in_kg']; $weight_category = array('Bantamweight' => 54,'Featherweight' => 57,'Lightweight'=> 60,'Light welterweight'=> 64,'Welterweight' => 67, 'Light Middleweight'=>71,'Middleweight' => 75,'Light heavyweight' => 81,'Cruiserweight' => 86,'Heavyweight' => 91,'Super Heavyweight' => 92); //**heaviest ever boxer was about 150 kilos if ($weight > 91) { $weight_category[0] + $weight; } elseif ($weight < $weight_category[$weight]) { echo "<p>{$weight_category[$weight]}</p>"; } else { echo "<p> invalid weight </p>"; } i know it's just a few lines, im a beginner and you dont have to give the solution maybe just some help. thanks the only other solution i can think of is with a load of ifs Quote Link to comment Share on other sites More sharing options...
noelm Posted January 19, 2011 Author Share Posted January 19, 2011 hi again guys, i was wondering if i could get some help. i have a form where the user has to enter his or her weight, and then the program will output their weight class. im having a few problems with this. im not sure how to tell php to check for the closest weight and give the answer. i'll give you a for instance: in the array i have ('featherweight' => 54) i need to tell php that everything below 53 is classed as featherweight then output "featherweight" $weight = $_GET['weight_in_kg']; $weight_category = array('Bantamweight' => 54,'Featherweight' => 57,'Lightweight'=> 60,'Light welterweight'=> 64,'Welterweight' => 67, 'Light Middleweight'=>71,'Middleweight' => 75,'Light heavyweight' => 81,'Cruiserweight' => 86,'Heavyweight' => 91,'Super Heavyweight' => 92); //**heaviest ever boxer was about 150 kilos if ($weight > 91) { $weight_category[0] + $weight; } elseif ($weight < $weight_category[$weight]) { echo "<p>{$weight_category[$weight]}</p>"; } else { echo "<p> invalid weight </p>"; } i know it's just a few lines, im a beginner and you dont have to give the solution maybe just some help. thanks the only other solution i can think of is with a load of ifs Quote Link to comment Share on other sites More sharing options...
btherl Posted January 31, 2011 Share Posted January 31, 2011 You could use a foreach loop like this: foreach ($weight_category as $category_name => $category_weight) { if ($weight < $category_weight) { # Match } } I'm not sure if you want "<" or "<=" for the comparison. As long as you have listed the maximum weights, and you have all the weights in ascending order, then this algorithm will work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.