Jump to content

How do I generate new random number if $_GET['script'] matces $random_num??


Jarod

Recommended Posts

Okay in my script I am getting the same output sometimes (the $_GET['script'] and $random_num) that match each other. I don't want them to match, so I'm trying to fix this so that $random_num doesnt match $_GET['script'].

private function getRandomScriptId() {
	$sql = mysql_query("SELECT id FROM script");
	$maximum = mysql_num_rows($sql);
	$minimum = ($maximum-$maximum)+1; // starts at 1
	$random_num = rand($minimum, $maximum);

	if($_GET['script'] == $random_num) { // I tried in here, but it didnt work either, I got a error sometimes
		new $random_num;
	} else {
		return $random_num;
	}
}

Link to comment
Share on other sites

if($_GET['script'] == $random_num) {
  getRandomScriptId();
} else {
  return $random_num;
}

 

that did not work before, I tried that same exact thing the first time I tried to fix this problem (and you forgot to return it btw).

Link to comment
Share on other sites

Should it be $this->getRandomScriptId()?

 

If that's not working, I suspect something else is wrong...

 

Also, why do you have $minimum = ($maximum-$maximum)+1;? Anything minus itself is 0, and 0+1 is always 1...

 

$minimum = 1;

Actually, it's not even really necessary to set a variable at all...

 

$random_num = rand(1, $maximum);

 

Also, if you're just checking the number of results from a query, why select anything from the table at all? Just do a count, it will be much faster and won't waste memory.

 

Where's $_GET['script'] defined?

Link to comment
Share on other sites

It will need to be prefixed with $this-> if the function is within a class & yeah, obviously it needs to be returned. Mistake on my part.

 

It's always helpful to let us know what you have tried when posting a question on the board, were not mind readers.

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.