Pete
Pete

Reputation: 170

Adding headers in HTML tables

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> \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;  

?>

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.

Upvotes: 0

Views: 42

Answers (3)

gbavba
gbavba

Reputation: 116

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> \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;
?>

Assuming the the \n'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.

Upvotes: 0

Condorcho
Condorcho

Reputation: 503

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

Upvotes: 1

Beny Hdez
Beny Hdez

Reputation: 92

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

use of \n with

1. echo directly to page

Now if you are trying to echo string to the page

echo  "kings \n 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 \n you need to use nl2br() function for that what it does is

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

echo  nl2br ("kings \n garden");

Output

kings
garden

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

so "\n" not '\n'

2. write to text file

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

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

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

output will be

kings
garden

Upvotes: 0

Related Questions