Link to home
Start Free TrialLog in
Avatar of impressionexpress
impressionexpress

asked on

php while not in_array()

I have an account number which basically a STRTOTIME. Now that I think about it was probably not the best idea but its too late I need to work with it for various reasons. In the db the account number is obviously a UNIQUE field. I really never thought to get a duplicate but it happen today.....

I created a function that verifies if the account number exists if it does I simply want to add 1 second.

What I have so far:
1. I got all the account numbers in the system and put them in an array
2. I check if() the newly generated account number is in the array

What I need to do now in the IF() is to create a loop that adds 1 second to the timestamp and check if its in the array.

I tried the following but doesnt seem to work:
	public function create_account_number($account_date, $application_time){
		global $Connection;
		
		// GET ALL ACCOUNT NUMBERS IN THE SYSTEM
		$all_account_numbers = array();
		$query = $Connection->query("SELECT account_number FROM accounts");
		while($result = mysqli_fetch_array($query, MYSQLI_ASSOC)){
			array_push($all_account_numbers, $result['account_number']);
		}
		
//		echo "<pre>";
//		print_r($all_account_numbers);
//		echo "</pre>";
		
		// POSSIBLE ACCOUNT NUMBER
		$account_number = strtotime($account_date . " " . $application_time);
		
		// ACCOUNT NUMBER EXISTS
		if(in_array($account_number, $all_account_numbers)){
			echo "In Array <br />";
			while(in_array(strtotime('+1 SECOND', $account_number), $all_account_numbers)){ 
				$account_number = strtotime('+1 SECOND', $account_number);
			}
		}
		return $account_number;
	}

Open in new window

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Don't bother using strtotime to add one second to a timestamp. A timestamp is just a number of seconds anyway, so just add 1:

OLD:
while(in_array(strtotime('+1 SECOND', $account_number), $all_account_numbers)){ 
	$account_number = strtotime('+1 SECOND', $account_number);
}

Open in new window


NEW:
while(in_array($account_number, $all_account_numbers)){ 
  $account_number++;
}

Open in new window


On a side note, it's never "too late" to change a bad data model. If you know of a foundational problem with your data like this, then the best time to fix it is ASAP. Otherwise, if you let it go, it will simply get worse and become even harder every day you let the problem fester.

Even this "fix" is burying you a little deeper in inefficiency that you'll have to strip out some day. It pulls all the account numbers into memory, which means you're doing a full query of those accounts, so the query will only get bigger and take up more memory as time progresses. If someday it hits the memory limit, then suddenly this process will break without any warning.

This is generally why auto increment columns are useful at the beginning. They can give you a unique new ID and that model will work for a good long while, and it can even work for a short time once you reach the point of multiple DB servers.
ASKER CERTIFIED SOLUTION
Avatar of impressionexpress
impressionexpress

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial