I am now creating a project. In this I am stuck. I want that an 8 digit pin number is generate. If this pin is already in the database then another pin number is generated. After counting 100 times if in all 100 times the generated pin number is in the database then a 9 digit pin number is generated. My Code is given below :
$t=$this->generate_string(8);
$th= Booking::Where('token',$t)->first();
$count = 0 ;
if(isset($th)) {
if($count >= 100){
$t = $this->generate_string(9);
}else{
$t = $this->generate_string(8);
}
$count++;
}
$booking->token = $t;
And generate_string function are
private function generate_string($length)
{
$character = '0123456789';
$character .= 'abcdefghijklmnopqrstuvwxyz';
$quantity_character = strlen($character);
$quantity_character--;
$Hash = NULL;
for ($x = 1; $x <= $length; $x++) {
$position = rand(0, $quantity_character);
$Hash .= substr($character, $position, 1);
}
return $Hash;
}
My code is not functioning properly. Please help me
Aucun commentaire:
Enregistrer un commentaire