Generating Unique Order Number

非 Y 不嫁゛ 提交于 2019-12-13 00:57:02

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/22345419/generating-unique-order-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!