Jump to content

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

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

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

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.