Advertisement
Guest User

Untitled

a guest
Jun 8th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. <?php
  2. $arr = array();
  3. $n = 5;
  4. $row = $n - 1;
  5. $col = 0;
  6. $row2;
  7. $count = 1;
  8.  
  9. for (;$row >=0; $row--)
  10. {
  11.     $row2 = $row;
  12.     $col = 0;
  13.     while ($row2 <= $n - 1 && $col <= $n - 1)
  14.     {
  15.         $arr[$row2][$col] = $count;
  16.         if($count == $n)
  17.         {
  18.             $arr[$row2][$col] = $n + 2;
  19.         }
  20.         elseif($count == $n+1)
  21.         {
  22.             $arr[$row2][$col] = $n + 1;
  23.         }
  24.         elseif($count == $n+2)
  25.         {
  26.             $arr[$row2][$col] = $n;
  27.         }
  28.  
  29.         $row2++;
  30.         $col++;
  31.         $count++;
  32.        
  33.     }
  34. }
  35.  
  36. $row = $n - 2;
  37. $col = $n - 1;
  38. $col2;
  39.  
  40.  
  41. for (;$row >=0 ; $row--)
  42. {
  43.     $col2 = $col;
  44.     $row2 = $row;
  45.    
  46.     while ($row2 >=0)
  47.     {
  48.         if ($count == $n*$n-2)
  49.         {
  50.             $arr[$row2][$col2] = $n*$n-1;
  51.         }
  52.         elseif ($count == $n*$n-1)
  53.         {
  54.             $arr[$row2][$col2] = $n*$n-2;
  55.         }
  56.         else
  57.         {
  58.             $arr[$row2][$col2] = $count;
  59.         }
  60.         $row2--;
  61.         $col2--;
  62.         $count++;
  63.     }
  64. }
  65. echo "<table border='1' style='border-collapse:collapse;text-align:center;'>";
  66. for ($i = 0; $i<$n; $i++)
  67. {echo "<tr>";
  68.     for($j = 0;$j<$n;$j++)
  69.     {
  70.         echo "<td style='width:40px;height:40px;padding:10px;'>";
  71.         echo $arr[$i][$j];
  72.         echo "</td>";
  73.     }
  74. }echo "<tr>";
  75. echo "</table>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement