在HTML表格中添加标题

时间:2017-03-14 14:54:51

标签: php html

我的任务是创建一个BMI表。

<?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> \n <head> \n <style> \n table, th, td {border: 1px solid black;} 
\n </style> \n </head> \n <body> \n <table style=width:100%> \n";  //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> \n </body> \n </html>"; //end table format
echo $tableStr;  

?>

我几乎可以使用它。唯一缺少的是在桌子顶部添加重量作为x轴。我试过了,我无法从计算中得到BMI结果,而且实际重量不会在桌子上显示出来。

我能够做到的唯一方法是创建一个单独的for循环并打印一行值,但我觉得应该能够在已经存在的嵌套for循环中做到。

3 个答案:

答案 0 :(得分:1)

试试这个:

$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>';

适合我,请在此处尝试:http://www.writephponline.com/(使用$minW$maxW的自定义值等。)

答案 1 :(得分:0)

\ n是换行符,但在html中我们使用&lt; br&gt;标签

使用\ n和

<强> 1。直接回显到页面

现在,如果您正在尝试将字符串回显到页面

echo  "kings \n garden";

输出

kings garden

您不会在新行中获得花园因为PHP是服务器端语言,并且您要将输出作为HTML发送,因此您需要在HTML中创建换行符。 Html不明白\ n你需要使用nl2br()函数来做它的作用

  

返回带&lt;的字符串br /&gt;或者&lt; br&gt;在所有换行符之前插入(\ r \ n,\ n \ r,\ n \ n和\ r \ n)。

echo  nl2br ("kings \n garden");

输出

kings
garden
  

注意确保用双引号回显/打印\ n,否则它   将按字面意思呈现为\ n。因为php解释器解析字符串   单引号中的概念

so "\n" not '\n'

<强> 2。写入文本文件

现在,如果您回显到文本文件,则只能使用\ n,它将回显到新的

$myfile = fopen("test.txt", "w+")  ;

$txt = "kings \n garden";
fwrite($myfile, $txt);
fclose($myfile);

输出

kings
garden

答案 2 :(得分:0)

我认为@ Condorcho的回答是有效的,但它并没有避免OP提到的额外循环。

要做到这一点,试试这个:

$minWeight = $_GET['min_weight']; //Getting values from HTML user input
$maxWeight = $_GET['max_weight'];
$minHeight = $_GET['min_height'];
$maxHeight = $_GET['max_height'];
$c = max(array($maxWeight-$minWeight,$maxHeight-$minHeight));

$aStr = "<html> \n <head> \n <style> \n table, th, td {border: 1px solid black;} 
\n </style> \n </head> \n <body> \n <table style=width:100%> \n <tr> \n <th></th>";
$bStr = "";

for($i = 0; $i <= $c; $i += 5){
if ($i<=($maxWeight-$minWeight)) {
    $aStr .= "<th>" . ($minWeight + $i) . "</th>";
}
if ($i<=($maxHeight-$minHeight)) {
    $bStr .= "<tr> \n <th>" . ($i + $minHeight) . "</th>";
    for($j = $minWeight; $j <= $maxWeight; $j += 5) {
        $bStr .= "<td>" . intval($j / pow((($i+$minHeight)/100),2)) . "</td>";
    }
    $bStr .= "\n </tr> \n";
}
} 
$aStr .= "\n </tr> \n";
$tableStr = $aStr . $bStr . "</table> \n </body> \n </html>";
echo $tableStr;
?>

假设\n用于格式化生成的源代码? @Beny是正确的,他们不会对呈现的HTML产生影响,如果这是意图的话,应该使用<br />