troubleshooting Question

php while not in_array()

Avatar of impressionexpress
impressionexpress asked on
PHP
2 Comments1 Solution19 ViewsLast Modified:
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;
	}
ASKER CERTIFIED SOLUTION
impressionexpress

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros