dongzhan2029 2017-03-14 14:54
浏览 355
已采纳

在HTML表格中添加标题

My task is to create a BMI table.

<?php

$minWeight = $_GET['min_weight']; //Getting values from HTML user input
$maxWeight = $_GET['max_weight'];
$minHeight = $_GET['min_height'];
$maxHeight = $_GET['max_height'];   

$tableStr = "<html> 
 <head> 
 <style> 
 table, th, td {border: 1px solid black;} 

 </style> 
 </head> 
 <body> 
 <table style=width:100%> 
";  //table formating

//This is ugly. I would like to merge this into the existing for loop
$tableStr .= "<th></th>";
for($j = $minWeight; $j <= $maxWeight; $j += 5) {
        $tableStr .= "<th>" . $j ."</th>";
}
//Up to here

for($i = $minHeight; $i <= $maxHeight; $i += 5){ //creating the number of headers
    $tableStr .= "<tr>"; 
    $tableStr .= "<th>" . $i . "</th>";
    for($j = $minWeight; $j <= $maxWeight; $j += 5) {
        //$tableStr .= "<th>" . $j ."</th>"; //print this alongside the line below messes up the table
        $tableStr .= "<td>" . intval($j / pow(($i/100),2)) . "</td>"; //This prints the result in the columns
    }   
    $tableStr .= "</tr>";
} 

$tableStr .= "</table> 
 </body> 
 </html>"; //end table format
echo $tableStr;  

?>

I have got it almost working. The only thing lacking is adding the weight on top of the table as an x-axis. I have tried, I can't get both the BMI results from the calculation and the actual weight untouched to show on the table.

The only way I have been able to do it was by creating a separate for loop and printing a row of the values, but I feel like one should be able to do inside the already existing nested for loop.

  • 写回答

3条回答 默认 最新

  • dtkwt62022 2017-03-14 15:25
    关注

    Try this:

    $minW = $_GET['min_weight'];
    $maxW = $_GET['max_weight'];
    $minH = $_GET['min_height'];
    $maxH = $_GET['max_height'];
    
    echo '<html><head><style>table, th, td { border: 1px solid black; } table { width: 100%; }</style></head><body>';
    echo '<table><th></th>';
    
    for ($i = $minH; $i <= $maxH; $i += 5) {
        // If we're on the first row, print the headers
        if ($i == $minH) {
            for ($j = $minW; $j <= $maxW; $j += 5) {
                echo '<th>' . $j . '</th>';
            }
        }
    
        echo '<tr>';
    
        for ($j = $minW; $j <= $maxW; $j += 5) {
            // If we're on the first column, print the row's number
            if ($j == $minW) {
                echo '<th>' . $i . '</th>';
            }
            echo '<td>' . intval($j / pow(($i/100),2)) . '</td>';
        }
    
        echo '</tr>';
    
    }
    
    echo '</table>';
    echo '</body></html>';
    

    Works for me, try it here: http://www.writephponline.com/ (using custom values for $minW, $maxW, etc..)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择