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

Link to comment
Share on other sites

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
Share on other sites

  • 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.