This question already has an answer here:
PHP sessions not working for game
(1 answer)
Closed 7 years ago.
I am making a simple game using PHP.
I need to store certain things like what round of the game is going on and what the total is etc and save this data and have access to it later(on another page or when I come back to this page)
I tried using sessions but this isn't working out for me.
Can I do this using cookies?
This is my first time using PHP.
Here is my code if it helps to understand:
<form method="get" action= "skunk.php">
<h1>PLAY SKUNK</h1>
ROLL AGAIN? <br>
yes<input type="radio" name="role2" value="yes"/>
no<input type="radio" name="role2" value="no"/>
<br>
<input type="submit"/>
</form>
<?php
session_start();
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
$_SESSION["n"] = array(0);
$_SESSION["k2"] = array(0);
$rand = 0;
$rand2 = 0;
$_SESSION["round"] = 1;
if($_REQUEST["role2"] == "yes"){
$rand = rand (1, 6);
$rand2 = rand(1, 6);
if($rand == 1 and $rand ==1){
switch ($_SESSION["round"]) {
case 1:
$_SESSION["s"] = array(0);
break;
case 2:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
break;
case 3:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
break;
case 4:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
$_SESSION["n"] = array(0);
break;
case 5:
$_SESSION["s"] = array(0);
$_SESSION["k1"] =array(0);
$_SESSION["u"] = array(0);
$_SESSION["n"] = array(0);
$_SESSION["k2"] = array(0);
break;
}
}
if($rand > 1 and $rand2 > 1) {
switch ($_SESSION["round"]) {
case 1:
array_push($_SESSION["s"], $rand, $rand2);
break;
case 2:
array_push($_SESSION["k1"], $rand, $rand2);
break;
case 3:
array_push($_SESSION["u"], $rand, $rand2);
break;
case 4:
array_push($_SESSION["n"], $rand, $rand2);
break;
case 5:
array_push($_SESSION["k2"], $rand, $rand2);
break;
}
$_SESSION["round"]++;
}
if($_SESSION["round"] > 5){
session_destroy();
}
echo "you rolled: " . $rand . "\n";
echo "you rolled: " . $rand2;
}
?>
<table style="width:100%" border="1" >
<tr>
<td>S</td>
<td>K</td>
<td>U</td>
<td>N</td>
<td>K</td>
</tr>
</table>
<table style="width:100%" border="1" >
<tr>
<td><?php echo array_sum($_SESSION["s"]); ?></td>
<td><?php echo array_sum($_SESSION["k1"]); ?></td>
<td><?php echo array_sum($_SESSION["u"]); ?></td>
<td><?php echo array_sum($_SESSION["n"]); ?></td>
<td><?php echo array_sum($_SESSION["k2"]); ?></td>
</tr>
</table>
I would use session only for user log status check, and store all other data into db it would make it easier to manage and more consistent.
Related
I'm working on a homework assignment while learning PHP for the first time. Everything is working, except for my switch statement. My professor is asking-
"Modify index.php
Get the selected value from the "calculate" radio button you created
Add a switch statement to set values for:
only the average calculation if the user selected the "average" radio button
only the total calculation if the user selected the "total" radio button
both the average and total if the user selected the "both" button."
I'd like to provide my entire index.php file in case it's important to see everything-
<?php
//set default values to be used when page first loads
$scores = array();
$scores[0] = 70;
$scores[1] = 80;
$scores[2] = 90;
$scores_string = '';
$score_total = 0;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;
$score_total_f = '';
$score_average_f = '';
//take action based on variable in POST array
$action = filter_input(INPUT_POST, 'action');
switch ($action) {
case 'process_scores':
$scores = $_POST['scores'];
// validate the scores
$is_valid = true;
for ($i = 0; $i < count($scores); $i++) {
if (empty($scores[$i]) || !is_numeric($scores[$i])) {
$scores_string = 'You must enter three valid numbers for scores.';
$is_valid = false;
break;
}
}
if (!$is_valid) {
break;
}
// process the scores
$score_total = 0;
foreach ($scores as $s) {
$scores_string .= $s . '|';
$score_total += $s;
}
$scores_string = substr($scores_string, 0, strlen($scores_string)-1);
// calculate the average
$score_average = $score_total / count($scores);
// format the total and average
$score_total_f = number_format($score_total, 2);
$score_average_f = number_format($score_average, 2);
$calculate = filter_input(INPUT_POST, 'calculate');
switch($calculate) {
case "average":
$message_average = $score_average_f;
break;
case "total":
$message_total = $score_total_f;
break;
case "both":
$message_average = $score_average_f;
$message_total = $score_total_f;
break;
default: die("Invalid type");
}
break;
case 'process_rolls':
$number_to_roll = filter_input(INPUT_POST, 'number_to_roll',
FILTER_VALIDATE_INT);
$total = 0;
$max_rolls = -INF;
for ($count = 0; $count < 10000; $count++) {
$rolls = 1;
while (mt_rand(1, 6) != $number_to_roll) {
$rolls++;
}
$total += $rolls;
$max_rolls = max($rolls, $max_rolls);
}
$average_rolls = $total / $count;
break;
}
include 'loop_tester.php';
?>
Also, here is part of the other file where I had to create radio buttons-
<h3>What do you want to do?</h3>
<input type="radio" name="calculate" value="average" checked> Average<br>
<input type="radio" name="calculate" value="total"> Total<br>
<input type="radio" name="calculate" value="both"> Both<br>
<label>Scores:</label>
<span><?php echo htmlspecialchars($scores_string); ?></span><br>
<label>Score Total:</label>
<span><?php echo $message_total; ?></span><br>
<label>Average Score:</label>
<span><?php echo $message_average; ?></span><br>
</form>
Thank you!
Again, everything is working fine when I test in XAMPP, just not the switch statement. I get no output of any kind.
EDIT: Disregard my original answer, I tested out your original syntax and it seems to work fine. Sorry, that was a mistake on my part, though I'd still say the new code is more elegant.
There seems to be an issue with a misplaced break; - Here is a full working code for to 'process_scores' case:
case 'process_scores':
$scores = $_POST['scores'];
// validate the scores
$is_valid = true;
for ($i = 0; $i < count($scores); $i++) {
if (empty($scores[$i]) || !is_numeric($scores[$i])) {
$scores_string = 'You must enter three valid numbers for scores.';
$is_valid = false;
break;
}
}
if (!$is_valid) {
break;
}
// process the scores
$score_total = 0;
foreach ($scores as $s) {
$scores_string .= $s . '|';
$score_total += $s;
}
$scores_string = substr($scores_string, 0, strlen($scores_string)-1);
// calculate the average
$score_average = $score_total / count($scores);
// format the total and average
$score_total_f = number_format($score_total, 2);
$score_average_f = number_format($score_average, 2);
$calculate = filter_input(INPUT_POST, 'calculate');
$score_average_f = number_format($score_average, 2);
$score_total_f = number_format($score_total, 2);
switch($calculate) {
case "average":
echo "Average: " . $score_average_f;
break;
case "total":
echo "Total: " . $score_total_f;
break;
case "both":
echo "Average: " . $score_average_f . "<br />";
echo "Total: " . $score_total_f;
break;
default: die("Invalid type");
}
break;
I'm not sure about other the other part of your code, but I tested this and got the intended results. If you still see nothing, check what's in your $_POST variables. Also as a general advice for debugging: in a situation like this, just go through your code and echo stuff out inside and outside of every loop or function you believe your code should reach, to see where it gets derailed. It may not sound too professional, but it sure gets the job done.
I'm doing the same exercise. You need to define your variables before all the code runs.
$scores_string = '';
$score_total = 0;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;
$message_average = 0;
$message_total = 0;
After I defined variables, $message_average and $message_total, everything worked fine.
I have a problem with correctly generating the days of the month, e.g. from 2018-10-01 to 2018-10-31 and checking if every day generated in the "for" loop is in the MySql database.
I can generate days but during the condition of date comparison from the generated loop with data from the database it displays only records from the database and how it needs to insert empty in the blank records Add date to create errors.
my code :
$conn = dbManager::getConnection();
$tr = 1;
for($tr; $tr < $day; $tr++){
$dt = new DateTime($start);
$msc = $dt->format('m');
switch ($msc) {
case '1':
$m = '1';
break;
case '2':
$m = '2';
break;
case '3':
$m = '3';
break;
case '4':
$m = '4';
break;
case '5':
$m = '5';
break;
case '6':
$m = '6';
break;
case '7':
$m = '7';
break;
case '8':
$m = '8';
break;
case '9':
$m = '9';
break;
case '10':
$m = '10';
break;
case '11':
$m = '11';
break;
case '12':
$m = '12';
break;
}
if($m < 10){
$mc = '0'.$m;
}else{
$mc = $m;
}
if($tr < 10){
$tr = '0'.$tr;
}else{
$tr = $tr;
}
$data = date('Y-'.$mc.'-'.$tr);
echo "<tr><td>".$data."</td><td style='vertical-align:middle;'>";
$result = $conn->query("SELECT * FROM open ")or die(mysqli_error());
while($row = mysqli_fetch_array($result)){
$Start = $row['Start'];
$End = $row['End'];
$dtb = new DateTime($Start);
$dtb_e = new DateTime($End);
$dtb_e->add(new DateInterval('PT1H'));
$w= $dtb->format('Y-m-d');
$ts = $dtb->format('H');
$te = $dtb_e->format('H');
if($w == $data){
for($h=1;$h<24;$h++){
if($h >= $ts && $h <= $te){
if($w < date('Y-m-d')){
$color ='gray';
}else{
$color = 'green';
}
echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
}else{
$color = 'gray';
echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
}
}
if($data >= date('Y-m-d')){
echo '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success" href="edit_termin.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Edytuj</a><a class="btn btn-sm btn-danger" href="del_open.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Usuń</a></div>';
}else{
echo '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success disabled" href="edit_termin.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Edytuj</a><a class="btn btn-sm btn-danger disabled" href="del_open.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Usuń</a></div>';
}
if($data != $data){
$dodaj = '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success" href="edit_termin.php?Start='.$data.'" role="button">Dodaj</a></div>';
echo $dodaj;
}else{
echo 'x';
}
}else{
if($data != $data){
$dodaj = '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success" href="edit_termin.php?Start='.$data.'" role="button">Dodaj</a></div>';
echo $dodaj;
}else{
echo 'x';
}
echo '<div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success" href="edit_termin.php?Start='.$data.'" role="button">Dodaj</a></div>';
}
}
}
echo "</td></tr>";
below result:
generated days without a button add
msg thanks for the help. I modified it according to your hints and works showing the button in the right place but it takes only the hours from the database fields from the last record. :( np.Start 2018-10-05 09:00 End 2018-10-05 20:00) script shows only these hours for each record found in the database and not those hours that are assigned to a given date .. Where can I make a mistake ?
$tr=1;
for($tr; $tr < $day; $tr++){
$dt = new DateTime($start);
$msc = $dt->format('m');
switch ($msc) {
case '1':
$m = '1';
break;
case '2':
$m = '2';
break;
case '3':
$m = '3';
break;
case '4':
$m = '4';
break;
case '5':
$m = '5';
break;
case '6':
$m = '6';
break;
case '7':
$m = '7';
break;
case '8':
$m = '8';
break;
case '9':
$m = '9';
break;
case '10':
$m = '10';
break;
case '11':
$m = '11';
break;
case '12':
$m = '12';
break;
}
if($m < 10){
$mc = '0'.$m;
}else{
$mc = $m;
}
if($tr < 10){
$tr = '0'.$tr;
}else{
$tr = $tr;
}
//$dt = new DateTime($start);
//$msc = $dt->format('m');
// $mc = $nsc; //strpad($msc, 2, '0');
//strpad($tr, 2, '0');
$data = date('Y-'.$mc.'-'.$tr);
echo '<tr><td>'.$data.'</td><td>';
// You should limit your results to the working month
$result = $conn->query("SELECT * FROM open ".$month." ")or die(mysqli_error());
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$db_dates = array_column($rows, 'Start');
$db_dates = array_map(function($data) {
$d = new DateTime($data);
return $d->format('Y-m-d');
}, $db_dates);
// Check if current date is not in the db result
$target_date = array_search($data, $db_dates);
if ($target_date !== FALSE) {
// Add your buttons
foreach ($rows as $key) {
$Start = $key['Start'];
$End = $key['End'];
$id = $key['ID'];
}
$str = $key['Start'];
$ed = $key['End'];
$dtb = new DateTime($str);
$dtb_e = new DateTime($ed);
$dtb_e->add(new DateInterval('PT1H'));
$w= $dtb->format('Y-m-d');
$ts = $dtb->format('H');
$te = $dtb_e->format('H');
for($h=1;$h<24;$h++){
if($h >= $ts && $h <= $te){
if($w < date('Y-m-d')){
$color ='gray';
}else{
$color = 'green';
}
echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
}else{
$color = 'gray';
echo '<p style="display:inline;padding:5px; border:1px solid ;background:'.$color.';color:#fff;border-radius: 5px;margin-right:5px;">'.$h.':00</p>';
}
}
//--------------------------------------
if($data >= date('Y-m-d')){
//-------------------------
$dst = $data.' '.$ts.':00';
$den = $data.' '.$te.':00';
//echo "SELECT * FROM open WHERE Start >= '".$dst."' AND End <= '".$den."' ";
$res = $conn->query("SELECT * FROM open WHERE Start >= '".$dst."' AND End <= '".$den."' ");
while($r = mysqli_fetch_array($res)){
$idr = $r['ID']; //ID record
echo '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success" href="edit_termin.php?Start='.$dst.'&End='.$den.'&ID='.$idr.'" role="button">Edutuj</a><a class="btn btn-sm btn-danger" href="del_open.php?Start='.$dst.'&End='.$den.'&ID='.$idr.'" role="button">Usuń</a></div></td>';
}
}else{
echo '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success disabled" href="edit_termin.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$idr.'" role="button">Edytuj</a><a class="btn btn-sm btn-danger disabled" href="del_open.php?Start='.$row['Start'].'&End='.$row['End'].'&ID='.$row['ID'].'" role="button">Usuń</a></div></td>';
}
} else {
// Column and map operations keep the indexes, access your record
$row = $rows[$target_date];
// Rest of your logic in here
if($data >= date('Y-m-d')){
echo '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success" href="form_terminarz.php?Start='.$data.'" role="button">Dodaj termin</a></div></td>';
}else{
echo '<td><div style="display:inline;text-align:rigth;margin-left:35px;"><a class="btn btn-sm btn-success disabled" href="form_terminarz.php?Start='.$data.'" role="button">Dodaj termin</a></div></td>';
}
}
echo '<td></tr>';
}
Hopefully this will get you going. The idea is to fetch all the results at once so you are able to check if the date is already in the database or not:
$conn = dbManager::getConnection();
$tr = 1;
$dt = new DateTime();
$msc = $dt->format('m');
$mc = str_pad($msc, 2, '0', STR_PAD_LEFT);
// You should limit your results to the working month
$result = $conn->query("SELECT * FROM open ")or die(mysqli_error());
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$db_dates = array_column($rows, 'Start');
$db_dates = array_map(function($v) {
$d = new DateTime($v);
return $d->format('Y-m-d');
}, $db_dates);
for($tr; $tr < $day; $tr++){
$tr = str_pad($tr, 2, '0', STR_PAD_LEFT);
$data = date('Y-'.$mc.'-'.$tr);
// Check if current date is not in the db result
$target_date = array_search($data, $db_dates);
if ($target_date === FALSE) {
// Add your buttons
} else {
// Column and map operations keep the indexes, access your record
$row = $rows[$target_date];
// Rest of your logic in here
}
}
My goal is to subtract the two times and then generate a logged in checker from there.
What I need to know, is if I have correctly subtracted the two times in minutes.
PHP Version: 7.0
Time is entered into the DB using NOW() and shows as (Example: 2016-07-23 15:01:34)
For some reason, where this code is included in HTML, is just blank.
Code (everything is defined higher up in the code) :
<?php
include ('../includes/connection.php');
include ('../scripts/functions.php');
$getOnlineAdmins = $handler->prepare('SELECT * FROM Admins WHERE AdminLevel >= :al AND Status= :stat');
$statn = 1;
$aln = 1;
$getOnlineAdmins->bindParam(':al', $aln);
$getOnlineAdmins->bindParam(':stat', $statn);
$getOnlineAdmins->execute();
echo "
<table class='table-fill'>
<thead>
<tr>
<th class='text-left' style='padding-left: 12px; padding-right: 12px;';></th>
</tr>
</thead>
<tbody class='table-hover'>";
if ($getOnlineAdmins->rowCount() >=1){
echo ("These is one or more rows.");
while ($results = $getOnlineAdmins->fetch()){
if((strtotime($results['LastVisited']) + 900) >= time()){
echo ("Time requirement met.");
switch ($results['AdminLevel']) {
case 3:
$rank = 'In-Training/Intern';
break;
case 6:
$rank = 'Community Moderator';
break;
case 9:
$rank = 'Senior Moderator';
break;
case 12:
$rank = 'Supervisor';
break;
case 15:
$rank = 'Administrator';
break;
case 18:
$rank = 'Senior Administrator';
break;
case 21:
$rank = 'Staff Advisor';
break;
case 24:
$rank = 'Engineer';
break;
case 27:
$rank = 'Vice Chairman';
break;
case 30:
$rank = 'Chairman';
break;
case 33:
$rank = 'Website Engineer';
break;
default:
$rank = 'Unknown';
break;
}
echo "<tr>";
echo "<td>" . $results['Username'] . " - " . $rank . "</td>";
} else {
echo "<tr><td>{$results['Username']} – $rank</td>";
}
}
}else{
echo "<tr><td>There are no staff members online.</td>";
}
echo " </tbody>
</table>";
?>
As far as I understand it, now() is not getting the current time as you expect it to. See this question for what I believe to be the root of your misunderstanding.
Assuming $results['LastVisited'] is in the format 2016-07-23 14:11:02 – you could convert it to a UNIX timestamp with strtotime.
So if the time 15 minutes ago is less than or equal to your last visited time:
if (strtotime('-15 minutes') <= strtotime($results['LastVisited'])) {}
Alternative format with time() – that you would use instead of the MySQL equivalent UNIX_TIMESTAMP():
if ((time() - 900) <= strtotime($results['LastVisited'])) {}
I have a coding matter i need your aid with. I have attempted to solve it for a few days, but all it results in is frustration. I haven't found any existing examples or help on the internet, and all i have done so far is staring at my code, my mind blank.
I need a countdown to 17:00 (5 PM) each day, echoing a message saying that a certain amount of time is remaining until 17:00. After 17:00, i want to echo another message. Then, the timer needs to reset after 23.59. How can i do this with PHP?
Any help would be appreciated! Below is the code i already have, but i don't think any part of it would affect what i need help with:
<?php
session_start();
#ADD ITEM
if( isset( $_POST['newitem'] ) ){
$_SESSION['todo'][]=$_POST['newitem'];
}
#REMOVE ITEM
if( isset( $_POST['remove_id'] ) ){
$id = $_POST['remove_id'];
unset( $_SESSION['todo'][$id] );
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$i = 0;
echo "<h1>To Do</h1>";
#SUBMIT
echo "
<form action='' method='post'>
<input type='text' name='newitem'>
<input type='submit'>
</form>
";
foreach( $_SESSION['todo'] as $id => $item ){
$i++;
#REMOVE
echo "
<form action='' method='post'>
<input type ='hidden' name='remove_id' value='$id'>
<input type='submit' value='-'>
$item
</form>";
}
echo $i;
#CHANGE BACKGROUND COLOR DEPENDING ON DAY
$day=date("l");
switch($day) {
case 'Monday':
$bg_color = "red";
break;
case 'Tuesday':
$bg_color = "blue";
break;
case 'Wednesday':
$bg_color = "purple";
break;
case 'Thursday':
$bg_color = "gray";
break;
case 'Friday':
$bg_color = "yellow";
break;
case 'Saturday':
$bg_color = "green";
break;
case 'Sunday':
default:
$bg_color = "beige";
break;
}
echo "<body style='background-color:$bg_color'>";
?>
</body>
</html>
I need a countdown to 17:00 (5 PM) each day, echoing a message saying
that a certain amount of time is remaining until 17:00. After 17:00, i
want to echo another message. Then, the timer needs to reset after
23.59. How can i do this with PHP?
if(date("H") < "17") echo "There are " . (16 - date("H")) . " hours and " . (60 - date("i")) . " minutes left until 17:00.";
else if(date("H") == "17" && date("i") == "00") echo "It is 17:00.";
else echo "17:00 is over for today.";
The calculation for the "female" option is found by multiplying her height in inches by 3.5 and subtracting 108. The calculation if "male" is selected will be found by multiplying his height in inches by 4 and subtracting 128. I am not sure how to translate this into a php function. This calculation is processed upon pushing a submit button. My goal is to calculate the ideal weight of the given gender and height.
Currently I am unable to display the $result. Does anyone see my error?
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
if(isset($_POST['submit'])) {
$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];
switch ($gender) {
case 2:
$result = ($height * 3.5) - 108;
break;
case 1:
$result = ($height * 4) - 128;
break;
default:
$result = 0;
}
echo "Ideal Weight:", $result;
}
?>
<html>
<div align="center">
<body>
<form name="form" method="post" action="<?php echo $PHP_SELF;?>">
Select Your Gender: <select name="gender">
<option value=""></option>
<option value="1">Male</option>
<option value="0">Female</option>
</select>
<br><br>
Enter Your Height: <input type="number" name="height">
<br><br>
<input type="submit" name="submit" value="Calculate"/>
</form>
</body>
</div>
</html>
Try this:
<?php
if(isset($_POST['submit'])) {
$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];
switch ($gender) {
case 1:
$multiplier = 3.5;
$substract = 108;
break;
case 0:
default:
$multiplier = 4;
$substract = 128;
break;
}
$result = ($height * $multiplier) - $substract;
//Do whatever you want to do with $result
}
?>
This is just a rough example, conversion to inches is not done, it's only to show a way to calculate the result.
Did I understand this correctly, is this the way you want to calculate the height?
if(isset($_POST['submit'])) {
$gender = isset($_POST['gender']) ? $_POST['gender']: 0;
$height = (int)$_POST['height'];
switch ($gender) {
case 2:
$result = ($height * 3.5) - 108;
break;
case 1:
$result = ($height * 4) - 128;
break;
default:
$result = 0;
}
echo $result;
}