0

I want to create two separate functions in php and in javascript when I fetch my primary key and I pass it to my php function it adds eight time 0 when value of primary key came up to 10 then 0 numbers should be seven and when it came to 100 then 0 should be six before my primary key of mysql and same separate function in javascript.

When I pass it my primary key the same functionality should work in also javascript I try to find at google but there is not any example for it.

I know there is an option in mysql called zero filling but I want to do that with php and javascript.

Melon
  • 874
  • 8
  • 17
user3833682
  • 263
  • 5
  • 20
  • php manual is your friend: [str_pad](http://php.net/str_pad) – Melon Mar 05 '15 at 09:54
  • thanks it worked in php but in javascript is there any function like `str_pad`? – user3833682 Mar 05 '15 at 10:04
  • what i recommend is once you converted your $id to 8 digit number instead of converting your id again in javascript pass already converted php $id value to javascript. – Shridhar Mar 05 '15 at 10:07

1 Answers1

3

in php you have two ways

1)sprintf('%08d', $id); 
and 
2)str_pad($id, 8, '0', STR_PAD_LEFT); 
Shridhar
  • 339
  • 2
  • 15
  • str_pad worked for me Melon tell me about this function thanks but how to do this in javascript? – user3833682 Mar 05 '15 at 10:08
  • if you have a value already converted using php then pass that value only to javascript or vice versa. you can refer http://stackoverflow.com/a/6466243/4599982 to javascript string formatting. – Shridhar Mar 05 '15 at 10:12