Jump to content

[SOLVED] Help


unidox

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/114110-solved-help/
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586498
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586504
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/114110-solved-help/#findComment-586577
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.