Jump to content

Help Generating 6 Digit Unique Numeric Code!


iPixel

Recommended Posts

Below is my code... I'm new to OOP so forgive me if it's poorly designed. What i'm trying to do is make sure that asset_key never get's duplicated. So when time comes to insert a new record, i can rest assures that asset_key will not duplicate.

 

For some reason $random_number never get's back a value;

 

<?php
class Randomize
{
	function createRN()
		{
			$num = rand(100000,999999);

			$sql = "SELECT * FROM mer_asset_header WHERE asset_key = '$num'";
			$go = oci_parse($conn, $sql);
			oci_execute($go);
			$count = oci_num_rows($go);
			if($count = 1)
				$repeat = Randomize::createRN();
			else
				return $num;
		}
}

$random_number = new Randomize;
$random_number->createRN();
?>

 

I should also mention, that if the query finds that asset_key, it should also run createRN() again untill it gets a unique one.

 

Thanks for the help in advance!

$return = $num;
        if($count >= 1)
	$return = Randomize::createRN();
else
	return $return;

 

The main thing wrong with your code is your use of $repeat.  It is never declared beforehand AND you never use it when you do declare it.

I did this but still nothing... i dont understand why am i using $num = Randomize::createRN();

 

<?php
class Randomize
{
	function createRN()
		{
			$num = rand(100000,999999);

			$sql = "SELECT * FROM mer_asset_header WHERE asset_key = '$num'";
			$go = oci_parse($conn, $sql);
			oci_execute($go);
			$count = oci_num_rows($go);

			if($count == 1)
				{
					$num = Randomize::createRN();
				}
			return $num;
		}
}

$random_number = new Randomize;
$random_number->createRN();
?>

 

Nevermind, i got it working... i just made a noobie mistake and was calling echo $random_number later on in the code after $random_number->createRN();

 

so i just did $random = $random_number->createRN() and echo $random whenever neede.

 

Thanks !!!

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.