在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.

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..)

is a line break but in html we use < br > tag

use of with

1. echo directly to page

Now if you are trying to echo string to the page

echo  "kings 
 garden";

output will be

kings garden

you wont get garden in new line because Since PHP is a server side language, and you are sending output as HTML, you need to create line breaks in HTML. Html dont understand you need to use nl2br() function for that what it does is

Returns string with < br / > or < br > inserted before all newlines ( , , and ).

echo  nl2br ("kings 
 garden");

Output

kings
garden

Note Make sure you're echoing/printing in double quotes, else it will be rendered literally as . because php interpreter parse string in single quote with concept of as is

so "
" not '
'

2. write to text file

now if you echo to text file you can use just and it will echo to new like

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

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

output will be

kings
garden

I think @Condorcho's answer will work but it doesn't avoid the extra loop as mentioned by the OP.

To do that try this:

$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> 
 <head> 
 <style> 
 table, th, td {border: 1px solid black;} 

 </style> 
 </head> 
 <body> 
 <table style=width:100%> 
 <tr> 
 <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> 
 <th>" . ($i + $minHeight) . "</th>";
    for($j = $minWeight; $j <= $maxWeight; $j += 5) {
        $bStr .= "<td>" . intval($j / pow((($i+$minHeight)/100),2)) . "</td>";
    }
    $bStr .= "
 </tr> 
";
}
} 
$aStr .= "
 </tr> 
";
$tableStr = $aStr . $bStr . "</table> 
 </body> 
 </html>";
echo $tableStr;
?>

Assuming the the 's are for formatting the source code produced? @Beny is correct that they won't make a difference to the rendered HTML and <br /> should be used instead if that's the intention.