sw41 Posted June 4, 2009 Share Posted June 4, 2009 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 https://forums.phpfreaks.com/topic/160852-solved-stagered-looping/ Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 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 Link to comment https://forums.phpfreaks.com/topic/160852-solved-stagered-looping/#findComment-848929 Share on other sites More sharing options...
sw41 Posted June 4, 2009 Author Share Posted June 4, 2009 That is awesome! It gets me the 1st half of the line: 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 1 1 8 7 6 5 4 3 2 How do I reverse the J loop to do the 2nd half? Link to comment https://forums.phpfreaks.com/topic/160852-solved-stagered-looping/#findComment-848945 Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 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 https://forums.phpfreaks.com/topic/160852-solved-stagered-looping/#findComment-848952 Share on other sites More sharing options...
sw41 Posted June 4, 2009 Author Share Posted June 4, 2009 Very Awesome, thanks!! Link to comment https://forums.phpfreaks.com/topic/160852-solved-stagered-looping/#findComment-849010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.