Jump to content

creating an array of different weight categories


noelm

Recommended Posts

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

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

  • 2 weeks later...

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.