/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help with table formula

Hey, Im having some issues with my code. I’m trying to produce a table based on the input of the x and y axis. I am able to get the information to show using for loops but the final formula only shows up on one line.

This is the formula i used to calculate the data meant to be in the table:

[code=php]number_format(($j/pow(($i/100),2)),3) [/code]

The formula works well but all the results appear on the last row instead of across the table.
Here’s my main code:

[code=php]
<?php

$min_weight = 60;
$max_weight = 90;
$min_height = 140;
$max_height = 170;

echo ‘<!DOCTYPE html>
<head>
<title>Task 1</title>
<style>
body {
background-color: blue;
}
</style>
</head>
<body>
<table border= “1” align= “center” style=”width:600px;”>
<tr>
<th>Height &#8674; </br> Weight &#8675;</th>’;
for($i=$min_height; $i<=$max_height; $i=$i+5){

echo ‘<th>’ . $i . ‘</th>
‘;
}
echo'</tr>’;
for ($j=$min_weight; $j<=$max_weight; $j= $j+5) {
echo ‘<tr>
<th>’ . $j . ‘</th>
‘;
}
for($i=$min_height; $i<=$max_height; $i=$i+5){
for ($j=$min_weight; $j<=$max_weight; $j= $j+5) {
echo ‘<td>’ . number_format(($j/pow(($i/100),2)),3) . ‘</td>
‘;
}
}
echo ‘
</tr>
</tr>
</table>
</body>
‘;
?>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 03.2015 — I think you want your last for loop inside of the second-to-last for loop?
Copy linkTweet thisAlerts:
@CodeGuruauthorMay 03.2015 — I think you want your last for loop inside of the second-to-last for loop?[/QUOTE]

I tried that but that just increases the first column and puts all values in one column
Copy linkTweet thisAlerts:
@rootMay 03.2015 — I don't see many <tr> to begin a row but plenty of <td>'s

so my advice is...

between [code=php]for($i=$min_height; $i<=$max_height; $i=$i+5){

for ($j=$min_weight; $j<=$max_weight; $j= $j+5) { [/code]

insert code to look something like...
[code=php]for($i=$min_height; $i<=$max_height; $i=$i+5){
echo "<tr>";

for ($j=$min_weight; $j<=$max_weight; $j= $j+5) { [/code]


and similarly, at the point [code=php]
}

}

echo '

</tr> [/code]

insert code to look like
[code=php]
}

echo "</tr>";
}

echo '

</tr> [/code]


See if that does anything for you, you can also swap $j= $j+5 for $j += 5, same for i...
Copy linkTweet thisAlerts:
@CodeGuruauthorMay 03.2015 — Hey thanks a lot for that. It helped to get it in order then i just changed the for loops around and it works perfectly now.
×

Success!

Help @CodeGuru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...