FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Test





AOP Web Development
Hello everyone... i just like to know if you how will you code by outputing this

1 10 11 20
2 9 12 19
3 8 13 18
4 7 14 17
5 6 15 16

that is inside in a html table...
there's no constant values..
looping can be use.


thanks.

Laughing Laughing Laughing Laughing
mathiaus
Came up with this quickly so its probably not that efficient but it works Smile

Code:
<?php

// Config Values
$rows = 5; // No. of rows wanted
$max = 20; // numbers from 1 to use (eg 1-20)

// Don't change
$num = 1;
$dir = 0; // 0-Down   1-Up
$i = 1;
$row = array();
$topple = $rows + 1;


while($num <= $max) {
   if($dir == 0) {
      $row[$i][] = $num;
      $i++;
      if($i == $topple) {
         $i = $rows;
         $dir = 1;
      }
   } elseif($dir == 1) {
      $row[$i][] = $num;
      $i--;
      if($i == 0) {
         $i = 1;
         $dir = 0;
      }
   }
   
   $num++;
}


echo '<table>';

$i = 1;
while($i <= $max) {
   echo '<tr>';
   $a=0;
   while($a < count($row[$i])) {
      echo '<td>'.$row[$i][$a].'</td>';
      $a++;
   }
   echo '</tr>';
   
   $i++;
}

echo '</table>';
   
?>
AOP Web Development
thanks bro for your code... how about if the output well be like this.. i hope you can make your code again

1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
kv
That is a fairly easy one than above
Code:

<table>
<?
for(i=1; i<=5; i++)
{
?>
<tr>
<td><? echo i ?></td>
<td><? echo 5 + i ?></td>
<td><? echo 10 + i ?></td>
<td><? echo 15 + i ?></td>
</tr>
<?
}
?>
</table>

mathiaus
Ordering that way is a LOT easier!

Use kv's code if you want precisely that, or mine if you want to be able to change it a bit more easily.

Code:
<?php

// Config Values
$rows = 5; // No. of rows wanted
$max = 20; // numbers from 1 to use (eg 1-20)

// Don't change
$num = 1;
$i = 1;
$row = array();
$topple = $rows + 1;


while($num <= $max) {
   $row[$i][] = $num;
   $i++;
   if($i == $topple) {
      $i = 1;
   }
   
   $num++;
}


echo '<table>';

$i = 1;
while($i <= $max) {
   echo '<tr>';
   $a=0;
   while($a < count($row[$i])) {
      echo '<td>'.$row[$i][$a].'</td>';
      $a++;
   }
   echo '</tr>';
   
   $i++;
}

echo '</table>';
   
?>
AOP Web Development
Great job! Smile Exclamation Exclamation
Related topics
PHP Test File
PHP: Feedback form issues.. help =P
Php and Other Wonderful Newbie Stories for Help.
PHP code for gradients.... (HEX) Color1, Color2, steps
php tutorial problem
PEAR path
I need a fool-proof anti-source view javascript.
"if" statements
Getting Hebrew User Input...
totally screwed by this unbeliaveable strange string!
BB Codes
Javascipt killin me!
How to write array in JS?
Pear
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.