Jump to content

Adding digits to beginning of number.


Veest

Recommended Posts

Heya,

 

My friend gave me this piece of PHP coding which should add extra 0's onto the beginning of a number depending on how large the number is. For example 0000001, 0000030, 0000658 etc etc.

 

However at the moment, all its doing is giving me the number without the extra 0's in!

 

while($l=mysql_fetch_array($result)){

$i=0;
while($i<1){
$id=$l[$i];

$ii=0;
$number='';
while($ii<(7-$id)){
$number.='0';
$ii++;
}
$number.=$id;
$out .='"Barcode","'.$id.'","LSO"';
$i++;
}

}

 

Does anyone have any ideas what is wrong with the coding? I'm sure its easy to fix for all you PHP Experts!

 

Thanks in advance!

Link to comment
Share on other sites

That code may or may not do what you want, but as soon as you reference the results as a number any leading zeros will be lost. You must always reference the result as a string in order to keep any leading zeros.

 

All that code can be replaced with the php str_pad() function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.