Hey guys please i am new to php mysql, i am trying to generate a code like this DEL-12345. So if the person chooses the state Delaware in the form, it generates and stores DEL-23413 in the db on submit. DEL (First 3 letters of the state); - (Hyphen) and then 12345 (5 random numbers)...
please help
I would like to point you in the right direction with the code below.
$Area = "Utah";
$Code = strtoupper(substr($Area, 0, 3)) . "-" . (mt_rand(1000,9999));
echo ( $Code );
However, note that this code is subject to errors for example: What if the $Area variable contained a string less than 3 characters? What if the random value generated is not unique (the theory being you want it to be)
All of this needs to be checked, by you, give it a go - we can help if needed but we wont write the whole code for you.
Good luck!
Use substr()
function to fetch first 3 letters from state, then mt_rand()
to generate random digit like this:
$state = "Delhi";
$code = strtoupper(substr($state, 0, 3)) . '-' . mt_rand(10000, 99999);
// Output "DEL-26845"
However you can find another more promising way to improve the random digit part to generate unique 5 random digit each time...