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;
	}
}

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).

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?

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.

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.