0

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.........

4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by ipradip
0

sample
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

0

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;
0

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.

0

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) {
    break;
  }
  $j++;
} while (0); //this is always true, so we have an infinite loop and you MUST have a breakout clause that will work

Edited by tiggsy: n/a

0

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')

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.