2

我将如何创建一个高度乘以重量的表格?

我不知道如何弄清楚!

我正在尝试创建一个 BMI 计算器。

// collect values from a form sent with method=get

$min_weight = mysql_real_escape_string($_GET["min_weight"]);
$max_weight = mysql_real_escape_string($_GET["max_weight"]);
$min_height = mysql_real_escape_string($_GET["min_height"]);
$max_height = mysql_real_escape_string($_GET["max_height"]);
?>

<table>
    <tr>
      <td>Key:</td>
      <td class="good show">Healthy (20-25)</td>
      <td cla ss="warning show">Overweight (25-30)</td>
      <td class="bad show">Unhealthy ( -20 or 30+)</td>
    </tr>
  </table>

<table border="1">
<?php 
echo "<tr><td>Height &rarr;<br>Weight &darr;</td>";
for ( $i = $min_height; $i <= $max_height; $i+=5 ) 
{
    echo "<td>{$i}</td>";
}
echo "</tr>";
for ( $j = $min_weight; $j <= $max_weight; $j+=5 ) 
{
    echo "<tr>";
    echo "<td>{$j}</td>";
    echo "</tr>";

}
4

2 回答 2

1

这应该可以完成这项工作,我还放了一些 isset():

<?php

// collect values from a form sent with method=get

$min_weight = isset($_GET["min_weight"]) ? mysql_real_escape_string($_GET["min_weight"]):0;
$max_weight = isset($_GET["max_weight"]) ? mysql_real_escape_string($_GET["max_weight"]):0;
$min_height = isset($_GET["min_height"]) ? mysql_real_escape_string($_GET["min_height"]):0;
$max_height = isset($_GET["max_height"]) ? mysql_real_escape_string($_GET["max_height"]):0;

?>

<table>
    <tr>
      <td>Key:</td>
      <td class="good show">Healthy (20-25)</td>
      <td class="warning show">Overweight (25-30)</td>
      <td class="bad show">Unhealthy ( -20 or 30+)</td>
    </tr>
  </table>

<?php
$table = '<table border="1">';
$table .= '<tr><td>Height &rarr;<br>Weight &darr;</td>';
for ($i = $min_height;$i <= $max_height;$i += 5){
    $table .= "<td>$i</td>";
}
$table .= "</tr>";
for($j = $min_weight; $j <= $max_weight;$j += 5){
    $table .= "<tr><td>$j</td>";
    for ($i = $min_height;$i <= $max_height;$i += 5){
        $table .= '<td>'. $i * $j .'</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';
echo $table;
?>
于 2013-03-19T22:24:18.077 回答
0

即使这样可行,将表格边框放在 php 标签之外是错误的吗?

<table>
    <tr>
      <td>Key:</td>
      <td class="good show">Healthy (20-25)</td>
      <td class="warning show">Overweight (25-30)</td>
      <td class="bad show">Unhealthy ( -20 or 30+)</td>
    </tr>
  </table>

<table border="1">
<?php 
echo "<tr><td>Height &rarr;<br>Weight &darr;</td>";

for ( $j = $min_height; $j <= $max_height; $j+=5 ) 
{
echo "<td>{$j}</td>";
}

for ( $i = $min_weight; $i <= $max_weight; $i+=5 ) 
{
echo "<tr><td>{$i}</td>";
for ( $j = $min_height; $j <= $max_height; $j+=5 ) 
{
$var = number_format(($i)/(($j/100)*($j/100)),'2','.','');
{
echo "<td class='warning'>";

}

echo $var;
echo "</td>";
}
echo "</tr>";
}
?>
</table>
于 2013-03-20T11:57:24.127 回答