drisate Posted October 25, 2009 Share Posted October 25, 2009 Hey guys i need to create a 00001 number in a loop. Is there a better way of forcing a 5 digit number then counting them and adding the missing zeros? Link to comment https://forums.phpfreaks.com/topic/178970-solved-adding-0-to-numbers/ Share on other sites More sharing options...
smerny Posted October 25, 2009 Share Posted October 25, 2009 $number = str_pad((int) $number,5,"0",STR_PAD_LEFT); Link to comment https://forums.phpfreaks.com/topic/178970-solved-adding-0-to-numbers/#findComment-944243 Share on other sites More sharing options...
drisate Posted October 25, 2009 Author Share Posted October 25, 2009 works but my code is not ... i am trying to increment 2 numbers and for some reason they both return 000 and 0000 WHILE ($counter1 < 1000){ $counter1 = str_pad((int) $counter1,3,"0",STR_PAD_LEFT); WHILE ($counter2 < 1000){ $counter2 = str_pad((int) $counter2,4,"0",STR_PAD_LEFT); echo $counter1."-".$counter2; $counter2++; } $counter1++; } Link to comment https://forums.phpfreaks.com/topic/178970-solved-adding-0-to-numbers/#findComment-944245 Share on other sites More sharing options...
smerny Posted October 25, 2009 Share Posted October 25, 2009 works but my code is not ... i am trying to increment 2 numbers and for some reason they both return 000 and 0000 WHILE ($counter1 < 1000){ $counter1 = str_pad((int) $counter1,3,"0",STR_PAD_LEFT); WHILE ($counter2 < 1000){ $counter2 = str_pad((int) $counter2,4,"0",STR_PAD_LEFT); echo $counter1."-".$counter2; $counter2++; } $counter1++; } with what i think you want to do, try something like this.. WHILE ($counter1 < 1000){ WHILE ($counter2 < 1000){ echo str_pad((int) $counter1,5,"0",STR_PAD_LEFT)."-".str_pad((int) $counter2,5,"0",STR_PAD_LEFT); $counter2++; } $counter1++; } also, since your counter2 does not reset... you're never going to really get to run through all the values i'm suspecting you were meaning to... maybe you want to put $counter2 = 1; directly between the two loop statements.... going to make a huge list of numbers... also, i'm guessing you'll want a line break or else it will just run all together Link to comment https://forums.phpfreaks.com/topic/178970-solved-adding-0-to-numbers/#findComment-944250 Share on other sites More sharing options...
drisate Posted October 25, 2009 Author Share Posted October 25, 2009 Yeah it's almost that. I am trying to get 1000 times 1000 the double loop you gave me never increments the seconde loop 00000-00001 00000-00002 00000-00003 00000-00004 [...] 00000-00999 i need it to start over the loop with 00002-00001 and so on untile i get 00999-00999 Link to comment https://forums.phpfreaks.com/topic/178970-solved-adding-0-to-numbers/#findComment-944255 Share on other sites More sharing options...
smerny Posted October 25, 2009 Share Posted October 25, 2009 well this is if you want to go from 1-1 to 1-999 and then 2-1 to 2-999 etc... seperated with a <br />... i'm sure you can figure out where to go from here if you want to change the initial values or something. $counter1 = 1; WHILE ($counter1 < 1000){ $counter2 = 1; WHILE ($counter2 < 1000){ echo str_pad((int) $counter1,5,"0",STR_PAD_LEFT)."-".str_pad((int) $counter2,5,"0",STR_PAD_LEFT)."<br />"; $counter2++; } $counter1++; } Link to comment https://forums.phpfreaks.com/topic/178970-solved-adding-0-to-numbers/#findComment-944262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.