5 Replies - 2525 Views - Last Post: 11 July 2013 - 09:05 PM Rate Topic: -----

#1 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Multiplication Table For Loop "x"

Posted 09 July 2013 - 08:51 PM

I'm making a multiplication table using PHP. I came up with this nested for-loop:
<?php
echo "<table border=2 width=30%";
for ($i = 1; $i <= 10; $i++ ) {  
	echo "<tr>";
	echo "<td>".$i."</td>";
	for ( $j = 2; $j <= 10; $j++ ) {
        	echo "<td>".$i * $j."</td>";
    	}
   	echo "</tr>";
}
echo "</table>";
?>



But I realized that it lacks "x" in the upper left corner:
x 1 2 3 4
1
2
3
4

How can I do it? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication Table For Loop "x"

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Multiplication Table For Loop "x"

Posted 09 July 2013 - 09:35 PM

I'm not sure this is the best way. But if you want to do it efficiently try using a multi-dimensional array.
<?php
echo "<table border=2 width=30%>";
//this will print the top part
echo "<tr>" 
echo "<td> x </td>";
for ($i = 1; $i <= 10; $i++) {
     echo "<td>" . $i . "</td>";
}
echo "</tr>";


for ($i = 1; $i <= 10; $i++ ) {  

	echo "<tr>";
	echo "<td>".$i."</td>";
	for ( $j = 2; $j <= 10; $j++ ) {
        	echo "<td>".$i * $j."</td>";
    	}
   	echo "</tr>";
}
echo "</table>";
?>


Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Multiplication Table For Loop "x"

Posted 09 July 2013 - 10:06 PM

View PostHypernal, on 10 July 2013 - 05:51 AM, said:

How can I do it?

use a table header (outside of your nested loop).
Was This Post Helpful? 1
  • +
  • -

#4 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: Multiplication Table For Loop "x"

Posted 09 July 2013 - 10:07 PM

View Postsalazar, on 09 July 2013 - 09:35 PM, said:

I'm not sure this is the best way. But if you want to do it efficiently try using a multi-dimensional array.
<?php
echo "<table border=2 width=30%>";
//this will print the top part
echo "<tr>" 
echo "<td> x </td>";
for ($i = 1; $i <= 10; $i++) {
     echo "<td>" . $i . "</td>";
}
echo "</tr>";


for ($i = 1; $i <= 10; $i++ ) {  

	echo "<tr>";
	echo "<td>".$i."</td>";
	for ( $j = 2; $j <= 10; $j++ ) {
        	echo "<td>".$i * $j."</td>";
    	}
   	echo "</tr>";
}
echo "</table>";
?>



But the inside values are messed up, it's not multiplication table anymore :|
Was This Post Helpful? 0
  • +
  • -

#5 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Multiplication Table For Loop "x"

Posted 10 July 2013 - 02:37 PM

How are the inside values messed up? I didn't change your code. I just added code to make the top axis (x-axis).

This should be the result.

x 1 2 3 4 5 6 7 8 9 10

Aren't you just printing the bottom part with your code?
Was This Post Helpful? 0
  • +
  • -

#6 kim_barcelona555  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 01-July 13

Re: Multiplication Table For Loop "x"

Posted 11 July 2013 - 09:05 PM

I have some modification to your code. Hope this helps :rockon:
<?php

echo "<table border=2 width=30%>";

for ($i = 1; $i <= 11; $i++ ) { 
$k=1;
   echo "<tr>";
   if($i==1)
	echo "<td>x</td>";
   else{
   $k=$i-1;
   echo "<td>$k</td>";
   }
   
    echo "<td>".$k ."</td>";
	
    for ( $j = 2; $j <= 10; $j++ ) {

            echo "<td>".$k * $j."</td>";

        }

    echo "</tr>";

}
    
echo "</table>";

?>



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1