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
Share on other sites

$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 />";

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.