Harsha M V
Harsha M V

Reputation: 54949

Generating Unique Order Number

I am trying to upload the cover image of a Facebook Page via Graph API, But it appears on the Timeline once its updated.

I am using the following string to check of my order number is unique

$unique_order_number = strtoupper(substr(md5(microtime()), 0, 8))

Now i want to just make sure that this number is not taken by any other order and wanna use a loop while or do-while loop to check the assignment.

public function __unique_order_no() {

    $order_no_exisits = $this->Order->find('first', array('order_no' => $unique_order_number));

    while (!empty($order_no_exisits)) {
        $unique_order_number = strtoupper(substr(md5(microtime()), 0, 8));
        $order_no_exisits = $this->Order->find('first', array('order_no' => $unique_order_number));
        debug($order_no_exisits);
    }

    debug($order_no_exisits);


}

How can i use a While Loop to run it till i get a unique number?

Upvotes: 1

Views: 1184

Answers (2)

Mehreen Jamil
Mehreen Jamil

Reputation: 34

while (!empty($order_no_exisits)) {
$unique_order_number = strtoupper(substr(md5(microtime()), 0, 8));
if(!$this->Order->find('first', array('order_no' => $unique_order_number)))
exit;
}

Upvotes: 1

Shairyar
Shairyar

Reputation: 3356

I needed to do something similar, a solution to keep unique id and i ended up with a solution to use PHP function time() like this $reference_number = 'BFF-' . time(); you can change the BFF to something that makes more sense to your business logic. This way i dont have to worry about if new id that is being generated was taken up before.

I hope this helps

Upvotes: 1

Related Questions