Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在HTML表中添加标题_Php_Html - Fatal编程技术网

Php 在HTML表中添加标题

Php 在HTML表中添加标题,php,html,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 <

我的任务是创建一个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循环中完成。

\n是换行符,但在html中我们使用
标记

与一起使用\n

1。直接回显到页面

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

echo  "kings \n garden";
输出将是

kings garden
kings
garden
因为PHP是一种服务器端语言,并且要以HTML的形式发送输出,所以需要在HTML中创建换行符。Html不理解\n您需要使用nl2br()函数,因为它的作用是

返回在所有换行符之前插入了

的字符串(\r\n\n\r\n和\r)

输出

kings
garden
注意确保用双引号\n回显/打印,否则 将按字面形式呈现为\n。因为php解释器解析字符串 在单引号中,概念为“原样”

2。写入文本文件

现在,如果您回送到文本文件,您可以只使用\n,它将回送到新文件,如

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

$txt = "kings \n garden";
fwrite($myfile, $txt);
fclose($myfile);
输出将是

kings garden
kings
garden
试试这个:

$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>';
$minW=$\u GET['min\u weight'];
$maxW=$\u GET['max\u weight'];
$minH=$\u获取['min\u高度'];
$maxH=$\u GET['max\u height'];
echo'表,th,td{边框:1px纯黑色;}表{宽度:100%;}';
回声';

对于($i=$minH;$i我认为@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;
?>
$minWeight=$\u GET['min\u weight'];//从HTML用户输入中获取值
$maxWeight=$\u GET['max\u weight'];
$minHeight=$\u获取['minHeight'];
$maxHeight=$\u GET['max\u height'];
$c=max(数组($maxWeight-$minWeight,$maxHeight-$minHeight));
$aStr=“\n\n\n表,th,td{边框:1px纯黑色;}
\n\n\n\n\n\n\n”;
$bStr=“”;
对于($i=0;$i)

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

我不是呈现文本文件,而是动态创建表。