KevBurgess Posted July 29, 2018 Share Posted July 29, 2018 Hi all, I am currently doing a PHP course and have the task of creating a multiplication table. The column headers are 10 wide and with 10 rows deep. The code I have created is as follows: <!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8" /> <title>Einsendeaufgabe 1</title> </head> <body> <h1>Einfaches 1mal1</h1> <table cellpadding = 15> <tr> <th></th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>10</th> </tr> <?php $row = 1; while ($row <= 10) { echo "<th>$row</th>"; $col = 1; while ($col <= 10) { $ergebnis = $row * $col; echo "<td>$ergebnis</td>"; $col ++; } $row ++; } ?> In the PHP code, one row above where I have incremented the variable $row, I have tried the following: <!--Attempt 1--> echo "\r\n"; <!--Attempt 2--> echo "<br>" . PHP_EOL; <!--Attempt 3--> <p> <?php //Code ?> </p> Nothing I have tried creates new rows and the result is all rows on a single line. Can anyone reied and clear up why this is happening? Many thanks for all your help in advance. Regards, Kev Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2018 Share Posted July 29, 2018 Your table body rows each need to begin and end with <tr> .. </tr>. (Just as your header row does) PS You will find for() loops are better than while() loops in this situation. Link to comment Share on other sites More sharing options...
KevBurgess Posted July 29, 2018 Author Share Posted July 29, 2018 Fantastic! That did the trick. Many thanks Barand...... This is the completed code for anyone that might be interested.... <!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8" /> <title>Einsendeaufgabe 1</title> </head> <body> <h1>Einfaches 1mal1</h1> <table cellpadding = 15> <tr> <th></th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>10</th> </tr> <?php $row = 1; while ($row <= 10) { echo "<tr><th>$row</th>"; $col = 1; while ($col <= 10) { $ergebnis = $row * $col; echo "<td>$ergebnis</td>"; $col ++; } echo "</tr>"; $row ++; } ?> I am going to re-write the code slightl to include the for-loop. Once I have that ready I will post it here. Regards, Kev Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted July 29, 2018 Share Posted July 29, 2018 Change the th to td Link to comment Share on other sites More sharing options...
Barand Posted July 29, 2018 Share Posted July 29, 2018 6 minutes ago, dodgeitorelse3 said: Change the th to td Why? What if it should look like this Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted July 29, 2018 Share Posted July 29, 2018 I stand corrected ? Link to comment Share on other sites More sharing options...
KevBurgess Posted July 30, 2018 Author Share Posted July 30, 2018 16 hours ago, Barand said: Why? What if it should look like this This is indeed the way it should look. Took me a while to square away as well.... Regards, Kev Link to comment Share on other sites More sharing options...
benanamen Posted August 3, 2018 Share Posted August 3, 2018 You are missing the closing table tag. You can also tidy up your posted code using range. <table cellpadding = "15"> <tr> <th></th> <?php foreach (range(1, 10) as $row) { echo "<th>$row</th>"; } ?> </tr> <?php foreach (range(1, 10) as $num) { echo "<tr><th>$num</th>"; foreach (range(1, 10) as $col) { $ergebnis = $num * $col; echo "<th>$ergebnis</th>"; } echo "</tr>"; } ?> </table> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.