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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊