Jump to content
KevBurgess

Problems with line breaks in a multiplication table

Recommended Posts

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

Edited by KevBurgess

Share this post


Link to post
Share on other sites

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.

Edited by Barand

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
6 minutes ago, dodgeitorelse3 said:

Change the th to td

 

Why?

What if it should look like this

 

Capture.PNG

Share this post


Link to post
Share on other sites
16 hours ago, Barand said:

Why?

What if it should look like this

 

Capture.PNG

This is indeed the way it should look.  Took me a while to square away as well.... :)

 

Regards,

Kev

Share this post


Link to post
Share on other sites

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>

 

Edited by benanamen

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.