Jump to content

[SOLVED] Stagered Looping?


sw41

Recommended Posts

If there an easy way to make a loop that makes this sequence of numbers where I define $x and then it sequences like below?

 

1 2 3 4 5 6 7 8 $x    $x 8 7 6 5 4 3 2 1

2 3 4 5 6 7 8 $x 1    1 $x 8 7 6 5 4 3 2

3 4 5 6 7 8 $x 1 2    2 1 $x 8 7 6 5 4 3

4 5 6 7 8 $x 1 2 3    3 2 1 $x 8 7 6 5 4

... and so on until all numbers are used

$x 1 2 3 4 5 6 7 8    8 7 6 5 4 3 2 1 $x

 

Thanks in advance!

Link to comment
Share on other sites

this should do it;

 

<?php
$x = isset($_GET['x']) ? $_GET['x'] : 8;
for($i=1; $i<=$x; $i++) {
for($j=$i; $j<$x+$i; $j++) {
	echo $j > $x ? $j-$x." " : "$j ";
}
echo '<br />';
}

 

 

your-page.php?x=8 - that will set x (if it isn't set the script defaults it to 8)

Link to comment
Share on other sites

not sure why, but i ignored taht for some reason

 

<?php
$x = isset($_GET['x']) ? $_GET['x'] : 8;
for($i=1; $i<=$x; $i++) {
for($j=$i; $j<$x+$i; $j++) {
	echo $j > $x ? $j-$x." " : "$j ";
}
$j--;
for($j; $j>=0+$i; $j--) {
	echo $j > $x ? $j-$x." " : "$j ";
}
echo '<br />';
}

Link to comment
Share on other sites

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.