9three Posted January 28, 2010 Share Posted January 28, 2010 Hey, I have a variable that holds an integer. I would like to get the values before that value but I'm having an issue. $current = 7; for($x = $current - 1; $x <= $current - 1 && $x > 0; --$x) echo $x; Output is: 654321 What I need is 123456. My eyes hurt, can someone help? Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/ Share on other sites More sharing options...
Aeglos Posted January 28, 2010 Share Posted January 28, 2010 You are thinking all backwards. You want the values FROM one and UP TO your defined boundary, just do: for ($x = 1; $x < $current; $x++) { echo $x; } So you loop from 1 up to one less than your $current value (that's why you use '<' instead of '<='). That should output 123456 if $current is 7. Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002758 Share on other sites More sharing options...
9three Posted January 28, 2010 Author Share Posted January 28, 2010 That won't work. If I'm on page 14, then I would need the last 5 values before 14. Output would be 13, 12, 11, 10, 9 If I use yours then it would always be 1, 2, 3, 4, 5 Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002761 Share on other sites More sharing options...
Alex Posted January 28, 2010 Share Posted January 28, 2010 <?php $current = 14; for($i = $current - 1;$i >= $current - 5 && $i > 0;$i--) { echo $i . "<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002763 Share on other sites More sharing options...
Aeglos Posted January 28, 2010 Share Posted January 28, 2010 I see now what you are attempting; getting the previous five integers. for ($x = $current-5; $x < $current; $x++) { if ($x < 1) { $x = 1; } echo $x; } That will give you the previous 5 integers before $current. Note the if() which is important if $current is lower than 5. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002769 Share on other sites More sharing options...
9three Posted January 28, 2010 Author Share Posted January 28, 2010 ah perfect! thank you Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002802 Share on other sites More sharing options...
9three Posted January 28, 2010 Author Share Posted January 28, 2010 Actually I take that back. I went with your code Alex and it displays it wrong. if current is 14 then output is: 13 12 11 10 9 14 It should be: 9 10 11 12 13 14 Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002803 Share on other sites More sharing options...
9three Posted January 28, 2010 Author Share Posted January 28, 2010 Ok I figured it out with the code you guys provided. Here is what I did. Any recommended improvement is always accepted $current = 16; if ($current <= 5) { for($i = 1;$i < $current; $i++) { echo $i.' '; } } else { for($i = $current - 5;$i <= $current - 1 && $i > 0; $i++) { echo $i.' '; } } echo ' Current:'.$current; Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002815 Share on other sites More sharing options...
lathifmca Posted January 28, 2010 Share Posted January 28, 2010 hi 9three, i m a newbie here, try this coding, this will help u for improvement <?php $x = 16; for( $i= $x-5; ($i < $x) && ($i > 0) ; $i++ ) { echo $i." "; } echo $x; ?> Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002840 Share on other sites More sharing options...
9three Posted January 28, 2010 Author Share Posted January 28, 2010 It's about the same except you dont have a less than or equal to. But thank you anyway. Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002844 Share on other sites More sharing options...
lathifmca Posted January 28, 2010 Share Posted January 28, 2010 hmm.. s, but in the above your code, u r checking an IF ELSE condition, that is unneccessary.. so that i replied that code... thats all... Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1002990 Share on other sites More sharing options...
9three Posted January 28, 2010 Author Share Posted January 28, 2010 Your code does not work if the x is less than 5, that's why I check if its less than 5. If it is, then I do a different for loop. Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1003049 Share on other sites More sharing options...
lathifmca Posted January 28, 2010 Share Posted January 28, 2010 oh right.. my mistake.. Quote Link to comment https://forums.phpfreaks.com/topic/190062-looping-reverse/#findComment-1003163 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.