0

我有一个需要 4 个字符的订单 ID 的 PHP/MySQL 电子商务网站。可以使用这样的递归方法:

private function getOrderId() {
    $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
    $num_chars = strlen($chars);
    $id_len = 4;
    $candidate_id = '';

    for($i = 0; $i < $id_len; ++$i) {
        $candidate_id .= $chars[mt_rand(0, $num_chars - 1)];
    }

    // check if $candidate_id exists using MySQL

    //
    if($it_exists) {
        return $this->getOrderId();
    }
    else {
        return $candidate_id;
    }
}
4

0 回答 0