我在新闻表中有一个主键字段(news_id)
从1,2,3,4等开始......
但是,我喜欢改为01,02,03,04,这是可能的吗?
如果没有,怎么能在PHP中完成?
答案 0 :(得分:14)
在99%的情况下,直接操作密钥是个坏主意。
最好的方法可能是在输出密钥时更改格式,如this question:
中所示$key = 4;
echo sprintf('%02d', $key); // outputs 04
答案 1 :(得分:0)
如果是输出,只需在主键小于10时添加0:
if($result['id'] < 10){
echo '0' . $result['id'];
}
答案 2 :(得分:0)
好的......另一种方法是......(虽然过于逻辑和数学......)
$id = 5;
$noOfZeros = 2; // i.e. 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. i.e. 5 gets converted to 0.05
$zeroedId = str_replace(".","",$id); // finally replace the "." with nothing... so 0.05 becomes 005 :).