Jump to content

Problems with line breaks in a multiplication table


KevBurgess

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

Link to comment
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

Link to comment
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>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.