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! Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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 />'; } Quote Link to comment Share on other sites More sharing options...
sw41 Posted June 4, 2009 Author Share Posted June 4, 2009 Very Awesome, thanks!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.