简体   繁体   中英

Table creation in php and html

I'm basically trying to perform a calculation with the first row and first column however I can't seem to get it working correctly.

function tableCreator($height_arrayy, $weight_arrayy) {


    echo "<table><tr><th></th>";

    foreach($height_arrayy as $value1) {

        echo "<th>$value1</th>";

    }

    echo "</tr>";



    foreach($weight_arrayy as $value2) {

        echo "<tr>";

        echo "<td>$value2</td>";

            foreach($weight_arrayy as $value3) {
                foreach($height_arrayy as $value4) {

                echo "<td>" . ($value3) / ( pow( ($value4 / 100), 2 ) ) . "</td>";

                }
            }

        echo "</tr>";


    }

    echo "</table>";



}       

Result I am getting:

这是我得到的结果

Desired Result:

我想要这样的东西

you have added an un-necessary foreach loop, update your second foreach loop as below

foreach($weight_arrayy as $value2) {
    echo "<tr>";
    echo "<th>$value2</th>";
    foreach($height_arrayy as $value4) {
        echo "<td>" . ($value2) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
    }
    echo "</tr>";
}

your code will return desired output. use round() if you want to round upto 2 or 3 decimal points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM