Bethphage Basketball Academy Bethphage Basketball Academy - 7 months ago 62
MySQL Question

How do i generate a code like this DEL-1234 in php

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!