Jump to content

Inverse of Do-While loop?


Recommended Posts

Is there any way to invert a Do-While loop so it continues to loop through the Do statement if the While statement is FALSE. And then end execution once the While statement is true. I guess I would basically need something like a Do-Until statement...

do{
$length = 2;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$string ="";
$string.= microtime(true);
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
$string= substr_replace($string, '', 0, 4);
for ($p = 0; $p < $length; $p++) {
$string2 .= $characters[mt_rand(0, strlen($characters))];
}
$string="".$string2."".$string;
$string=explode(".",$string);
$string=$string[0].$string[1];
$rma_number=$string;

$sql12345="SELECT * FROM $tbl_name4 WHERE rma_number=$rma_number";
$result12345=mysql_query($sql12345);
$num_rows12345=mysql_num_rows($result12345);
}
while($num_rows12345 < 1){
$sql2000000="UPDATE $tbl_name4 SET rma_number='$rma_number', rma_name='$rma_name' WHERE rma_id = $rma_id";
mysql_query($sql2000000);
}

Link to comment
Share on other sites

Alright I'm going crazy trying to figure out if this would work or not. I found a do-while script on the net and altered it slightly:

 

<?
$cookies = 0;
do{
echo "Mmmmm...I love cookies! *munch munch munch*";
$cookies++;
} 
while ($cookies != 1);

if($cookies==1){
echo "test test";
}
?>

 

The above executes once when $cookies=0, and goes crazy and fills the page (continues executing) when equal to or above 1. Is this what I'm looking for?

Link to comment
Share on other sites

well of course it does. You have it set to execute as long as cookie is not equal to 1. This means that as long as its not 1, it will keep executing, and since you keep incrementing it, if it goes above one it will execute indefinitely. WHen you set it to 0, it executes once, becomes 1, then stops. However, if you set it to anything >= 1, it will always become greater than 1 at the end of the loop, even in the first execution of the do block, and execute endlessly

 

What exactly are you trying to do? How many times do you want this loop to execute given a certain amount of cookie to start?

Link to comment
Share on other sites

Going back to my original code... I need it to generate a random number/key, search the Database, if there's already an entry with the same key, rerun the key generation. If there are no duplicate entries insert the key generated.

 

So I'm thinking do{} while($num_rows!=1); should work. It should, if there's no duplicates, insert the key, loop back around and redo the do statement, and catch that there's now an entry and stop executing... correct?

 

So:

 

do{
$length = 2;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$string ="";
$string.= microtime(true);
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
$string= substr_replace($string, '', 0, 4);
for ($p = 0; $p < $length; $p++) {
$string2 .= $characters[mt_rand(0, strlen($characters))];
}
$string="".$string2."".$string;
$string=explode(".",$string);
$string=$string[0].$string[1];
$rma_number=$string;

$sql12345="SELECT * FROM $tbl_name4 WHERE rma_number=$rma_number";
$result12345=mysql_query($sql12345);
$num_rows12345=mysql_num_rows($result12345);
}
while($num_rows12345 != 1);

if($num_rows12345==0){
$sql2000000="UPDATE $tbl_name4 SET rma_number='$rma_number', rma_name='$rma_name' WHERE rma_id = $rma_id";
mysql_query($sql2000000);
}

Link to comment
Share on other sites

okay well you should be doing something more like do{} while ($num_rows > 0);  Because you want to keep trying to generate a new number while the generated number is found.

 

But anyways, looking at your original code...you prefix this string with a microtime timestamp, followed by a random string.  I don't know what you are actually using these random strings for, but with the way you are generating it, the chances of generating a duplicate string are so small that IMO it's not even worth checking.  I mean seriously, two people would have to submit it at the exact same microtime level moment in time, server would have to process both requests at the exact same moment in time, and by some miracle, follow up by generating the exact same random string that's appended to the microtime. 

 

The only way that could even begin to have a 1.e-100000000 or some shit chance to actually happen would be if you have massive amounts of people constantly executing this script and then maybe MAYBE it might eventually happen.  But if you are actually operating under those conditions, this loop that checks for existing string isn't going to really work anyways, because it would be equally conceivable that the string could be duped between check and insert.  So you would have to at a minimum lock your table while performing the check and insert, so that no other values can be inserted.  This is going to slow things down, esp if you are really expecting lots of hits like that.

 

 

Basically what I'm trying to say is that with the way you are generating the string, I wouldn't even worry about checking if it's been duped.  Especially depending on what these values are actually being used for...

 

edit:

 

hmm...well, I guess I didn't fully look at your code.  Seems like you start out by prefixing with microtime, but then do stuff after that that takes the main use for it out of the picture.  Is there a reason you can't make this "random string" keep the full microtime prefix?

 

Link to comment
Share on other sites

edit:

 

hmm...well, I guess I didn't fully look at your code.  Seems like you start out by prefixing with microtime, but then do stuff after that that takes the main use for it out of the picture.  Is there a reason you can't make this "random string" keep the full microtime prefix?

 

 

I'm taking off the first 4 digits of microtime, which are pretty much always the same. And appending the randomly generated letters and numbers to the front and back. The first 4 digital of microtime should be 1306 for the foreseeable future. I could keep the full microtime + generate letters and numbers for the beginning and end, I was just trying to keep it under 12 characters. But that's really not all the necessary, I guess.

Link to comment
Share on other sites

Just went with this instead.

 

if($rma_issued=="y"){
$length = 2;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$string= microtime(true);
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
for ($p = 0; $p < $length; $p++) {
$string2 .= $characters[mt_rand(0, strlen($characters))];
}
$string="".$string2."".$string;
$string=explode(".",$string);
$string=$string[0].$string[1];
$rma_number=$string;
$sql2000000="UPDATE $tbl_name4 SET rma_number='$rma_number', rma_issued='$rma_issued' WHERE rma_id = $rma_id";
mysql_query($sql2000000);
header("Location: ./acp_admincp.php?rma=issued");
}

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.