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 Link to comment https://forums.phpfreaks.com/topic/225000-creating-an-array-of-different-weight-categories/ 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 Link to comment https://forums.phpfreaks.com/topic/225000-creating-an-array-of-different-weight-categories/#findComment-1162114 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. Link to comment https://forums.phpfreaks.com/topic/225000-creating-an-array-of-different-weight-categories/#findComment-1167560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.