i want to generate A0001 as user id in php. i tried a lot for this but i can't did that so anybody here to help me.........

Recommended Answers

All 7 Replies

select concat('A',lpad(MAX(substr('A0001',2))+1,4,'0')) new_number

from table here pkcolname is your primary key of table with first is letter and rest 4 are numbers.
select concat('A',lpad(MAX(substr(pkcolname,2))+1,4,'0')) new_number FROM TABLENAME

You don't give a lot away do you?

You want to generate (as opposed to assign) 'A0001' as user id - so presumably you will also be generating other ids of, what, A0002, A0003 and so on? or B0001, C0001?

Either way, what you need to do is to generate the numeric part and (if the alpha part changes generate that as well), then stick them together.

$prefix = "A";
$suffix = 1;
$id = $prefix.sprintf("%04s",$suffix);
echo $id;

tiggsy that code really works but i want to connect database though this so it should generate A0002 and so on after A0001 so if you can then provide me tat code.

Well, you just stick the code in a loop,

Set your maximum value and do it like this:

for ($j=1;$j<=$maximumvalue;$j++) {
  $suffix = $j;
  $id = $prefix.sprintf("%04s",$suffix);
  //rest of your code for each record goes here

or if you don't know the maximum you could do it like this:

$j = 1;
do {
  $suffix = $j;
  $id = $prefix.sprintf("%04s",$suffix);
  //rest of your code for each record goes here
  if (whatever condition means you reached the end) {
} while (0); //this is always true, so we have an infinite loop and you MUST have a breakout clause that will work

Let mysql do it for you. Just use a PK BIGINT(20) AUTOINC (id) field, and add a calculated field to transform it into your id. CONCAT('A', LPAD(CAST(id AS VARCHAR)), 4, '0')

thanks guys......... thanks for your help .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.