unidox Posted July 10, 2008 Share Posted July 10, 2008 This is my code: $i = 0; $allow = "abcdefghijkmnpqrstuvwxyz23456789"; while ($i < 18) { // Generates 8 Digit Activation Number srand((double)microtime()*1000000); for($i=0; $i<24; $i++) { $activation .= $allow[rand()%strlen($allow)]; } echo $activation . "<br />"; $i++; } What I am trying to do it output 18 lines of activation codes. But its only outputting one line. How do I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/ Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 $i = 0; $allow = "abcdefghijkmnpqrstuvwxyz23456789"; while ($i < 18) { // Generates 8 Digit Activation Number srand((double)microtime()*1000000); for($i=0; $i<24; $i++) { $activation .= $allow[rand()%strlen($allow)]; } $i++; } echo $activation . "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586495 Share on other sites More sharing options...
rmbarnes82 Posted July 10, 2008 Share Posted July 10, 2008 Hi, Your using the variable $i to mean two different things, and this is where your problem comes from. Your while loop runs the first time, when $i ==0. Then the for loop runs, after which $i==23 (you increment $i up to 23 in the for loop). Then you print the first activation code, then increment $i again, so now $i==24. The code jumps to the start of the while loop, but since $i is now 24 the $i < 18 fails, and the loop terminates. This code should fix your problem: <?php $i = 0; $allow = "abcdefghijkmnpqrstuvwxyz23456789"; while ($i < 18) { // Generates 8 Digit Activation Number srand((double)microtime()*1000000); for($y=0; $y<24; $y++) { $activation .= $allow[rand()%strlen($allow)]; } echo $activation . "<br />"; $i++; } Robin Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586498 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Hehe, I didn't even notice the $i var being used over. You'll still want to move the echo outside the while though. Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586500 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Well, thorpe, then it'll just be one long 18*24 character long string since he has no new lines. What he should do is reset the $activation variable for each while iteration: $i = 0; $allow = "abcdefghijkmnpqrstuvwxyz23456789"; while ($i < 18) { $activation = ''; // Generates 8 Digit Activation Number srand((double)microtime()*1000000); for($y=0; $y<24; $y++) { $activation .= $allow[rand()%strlen($allow)]; } echo $activation . "<br />"; $i++; } Or make an array and then work with that, but this way it easier I think. Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586504 Share on other sites More sharing options...
rmbarnes82 Posted July 10, 2008 Share Posted July 10, 2008 Yeah, I only noticed one bug, not the echo bug also. Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586510 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Easier. $activation = $allow[rand()%strlen($allow)]; Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586513 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 @thorpe: He's supposed to be adding 1 character for each iteration of FOR. Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586515 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Hehe, thats what I get for posting while I'm meant to be working. Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586572 Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Nice. Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586573 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2008 Share Posted July 10, 2008 Then there is a simpler way to do this: <?php for ($i=0;$i < 18;$i++) { $allow = array_merge(range('a','z'),range(2,9)); shuffle($allow); $activation = array_slice($allow,0,24); echo '<pre>' . implode('',$activation) . "</pre>"; } ?> I believe it produces similar strings. Ken Quote Link to comment https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586577 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.