forked from pimteam/BMI-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overweight_calculator.php
178 lines (156 loc) · 7.29 KB
/
overweight_calculator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/* This calculator is your for free by Calendarscripts.info. You have no obligations for anything - you can modify, redistribute, sell it or whatever you want to do.
We will appreciate if you don't remove the link at the bottom, but that's not required. */
/* Feel free to modify the CSS and the texts below. - no problem at all. Just don't touch the PHP code or the specual codes which are surrounded with %% unless you know what you are doing. */
session_start();
$firsttext="Your Body Mass Index (BMI) is %%BMI%%. This means your weight is within the %%BMIMSG%% range.";
$normaltext="You seem to keep your weight normal. Well done!";
$lowertext="Your current BMI is lower than the recommended range of <strong>18.5</strong> to <strong>24.9</strong>. <br>To be within the right range for your height, you should weigh between <strong>%%LOWERLIMIT%% lbs</strong> / <strong>%%LOWERLIMITKG%% kg</strong> and <strong>%%UPPERLIMIT%% lbs</strong> / <strong>%%UPPERLIMITKG%% kg</strong>";
$uppertext="Your current BMI is greater than the recommended range of <strong>18.5</strong> to <strong>24.9</strong>. <br>To be within the right range for your height, you should weigh between <strong>%%LOWERLIMIT%% lbs</strong> / <strong>%%LOWERLIMITKG%% kg</strong> and <strong>%%UPPERLIMIT%% lbs</strong> / <strong>%%UPPERLIMITKG%% kg</strong>";
?>
<style type="text/css">
.calculator_div
{
font-size:11px;
font-family:verdana, arial, sans-serif;
border:2pt solid #4444FF;
padding:25px;
margin:auto;
width:300px;
}
.calculator_div label
{
display:block;
float:left;
width:100px;
}
</style>
<?php
if(!empty($_POST['calculator_ok']))
{
// set vars in session
foreach($_POST as $key=>$var)
{
$_SESSION['bmi_calc_'.$key]=$var;
}
if($_POST['system']=='english')
{
$height=$_POST['height_ft_en']*12+$_POST['height_in_en'];
$bmi=($_POST['weight_en']*703) / ($height*$height);
}
else
{
$height=$_POST['height_met']/100;
$bmi=$_POST['weight_met'] / round(($height*$height),2);
}
$bmi=number_format($bmi,1,".","");
// prepare message for the user
if($bmi<=18.5)
{
$bmimsg="Underweight";
}
if($bmi>18.5 and $bmi<=24.9)
{
$bmimsg="Normal";
}
if($bmi>=25 and $bmi<=29.9)
{
$bmimsg="Overweight";
}
if($bmi>=30)
{
$bmimsg="Obese";
}
// what is the weight range?
if($bmimsg!='Normal')
{
if($_POST['system']=='english')
{
$lowerlimit=number_format(( 18.5 * ($height*$height) ) / 703);
$lowerlimitkg=number_format($lowerlimit*0.453,1,".","");
$upperlimit=number_format(( 24.9 * ($height*$height) ) / 703);
$upperlimitkg=number_format($upperlimit*0.453,1,".","");
}
else
{
$lowerlimit=number_format( 18.5 * ($height*$height) * 2.204 );
$lowerlimitkg=number_format(18.5 * ($height*$height),1,".","");
$upperlimit=number_format( 24.9 * ($height*$height) * 2.204 );
$upperlimitkg=number_format(24.9 * ($height*$height),1,".","");
}
}
//prepare texts
$firsttext=str_replace("%%BMI%%",$bmi,$firsttext);
$firsttext=str_replace("%%BMIMSG%%",$bmimsg,$firsttext);
$lowertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$lowertext);
$lowertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$lowertext);
$lowertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$lowertext);
$lowertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$lowertext);
$uppertext=str_replace("%%LOWERLIMIT%%",$lowerlimit,$uppertext);
$uppertext=str_replace("%%LOWERLIMITKG%%",$lowerlimitkg,$uppertext);
$uppertext=str_replace("%%UPPERLIMIT%%",$upperlimit,$uppertext);
$uppertext=str_replace("%%UPPERLIMITKG%%",$upperlimitkg,$uppertext);
}
?>
<form method="post">
<div class="calculator_div">
<div><input type="radio" value="english" name="system" <?php if($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english') echo "checked='true'";?> onclick="changeSystem('english');"> English
<input type="radio" value="metric" name="system" <?php if($_SESSION['bmi_calc_system']!='' and $_SESSION['bmi_calc_system']=='metric') echo "checked='true'";?> onclick="changeSystem('metric');"> Metric</div>
<div><label>Your Weight:</label>
<span id="englishWeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'block':'none'?>;"><input type="text" name="weight_en" size="6" value="<?php echo !empty($_SESSION['bmi_calc_weight_en'])?$_SESSION['bmi_calc_weight_en']:""?>"> lbs</span>
<span id="metricWeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=="" or $_SESSION['bmi_calc_system']=='english'))?'none':'block'?>;"><input type="text" name="weight_met" size="6" value="<?php echo !empty($_SESSION['bmi_calc_weight_met'])?$_SESSION['bmi_calc_weight_met']:""?>"> kg</span>
</div>
<div><label>Your Height:</label>
<span id="englishHeight" style="display:<?php echo (($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english'))?'block':'none'?>;"><input type="text" size="6" name="height_ft_en" value="<?php echo !empty($_SESSION['bmi_calc_height_ft_en'])?$_SESSION['bmi_calc_height_ft_en']:""?>"> ft
<input type="text" size="6" name="height_in_en" value="<?php echo ($_SESSION['bmi_calc_height_in_en']!='')?$_SESSION['bmi_calc_height_in_en']:""?>"> in</span>
<span id="metricHeight" style="display:<?php echo ($_SESSION['bmi_calc_system']=='' or $_SESSION['bmi_calc_system']=='english')?'none':'block'?>;">
<input type="text" name="height_met" size="6" value="<?php echo ($_SESSION['bmi_calc_height_met']!='')?$_SESSION['bmi_calc_height_met']:""?>"> cm
</span>
</div>
<div align="center">
<input type="hidden" name="calculator_ok" value="ok">
<input type="submit" value="Are You Overweight?">
</div>
<div align="center"><a href="http://calendarscripts.info/bmr-calculator.html">BMR calculator</a></div>
</div>
</form>
<?php if(!empty($_POST['calculator_ok'])):?>
<div class="calculator_table">
<p><?=$firsttext?></p>
<?php
switch($bmimsg)
{
case 'Normal':
// you can echo here if you want for normal weight people
break;
case 'Underweight':
echo $lowertext;
break;
default:
echo $uppertext;
break;
}
?>
<p align="center"><a href="http://<?=$_SERVER['HTTP_HOST'];?><?=$_SERVER['REQUEST_URI']?>">Calculate again</a></p>
</div>
<?php endif;?>
<script type="text/javascript">
function changeSystem(s)
{
if(s=='english')
{
document.getElementById('englishWeight').style.display='block';
document.getElementById('englishHeight').style.display='block';
document.getElementById('metricWeight').style.display='none';
document.getElementById('metricHeight').style.display='none';
}
else
{
document.getElementById('englishWeight').style.display='none';
document.getElementById('englishHeight').style.display='none';
document.getElementById('metricWeight').style.display='block';
document.getElementById('metricHeight').style.display='block';
}
}
</script>