The primary key is from 1 to 01, from 2 to 02, etc.

I have a primary key field (news_id) in the news table

starts with 1, 2, 3, 4, etc.

However, I like to change to 01, 02, 03, 04, e tc ... maybe?

If not, how can this be done in PHP?

+4
source share
3 answers

Manipulating keys directly is a bad idea in 99% of cases.

The best way to go is probably to change the format when outputting keys, as shown in this question:

$key = 4; echo sprintf('%02d', $key); // outputs 04 
+14
source

If this is for output, just add 0 if the primary key is less than 10:

 if($result['id'] < 10){ echo '0' . $result['id']; } 
0
source

Okay ... Another way to do this ... (although too logical and mathematical ...)

 $id = 5; $noOfZeros = 2; // ie you are converting 2 to 002. $divider = pow(10,$noOfZeros); // Now you are creating a divider (/100 in this case) $id = $id / $divider; // dividing the id by 100. ie 5 gets converted to 0.05 $zeroedId = str_replace(".","",$id); // finally replace the "." with nothing... so 0.05 becomes 005 :). 
0
source

All Articles