Log in

View Full Version : Leading zeros



pavmoxo
05-12-2006, 09:00 AM
I have the next code:


<select name="Ehour" id="Ehour" tabindex="3">

<?php
for($n=0; $n <=24; $n++)
{
?>
<option value="<? echo $n ?>"><? echo $n ?></option>
<?
}
?>

</select>

I want this drop-dow show leading zeros for hours. Do you know how to do it??

mwinter
05-12-2006, 11:08 AM
I want this drop-dow show leading zeros for hours. Do you know how to do it??Use the str_pad (http://uk.php.net/str_pad) function. For example:



str_pad($n, 2, '0', STR_PAD_LEFT)
Mike


Please don't post such wildly-formatted code. Edit it so that, at the very least, it's not necessary for readers to scroll horizontally just to read it all.

pavmoxo
05-16-2006, 10:07 AM
In this code:


<option value="<? echo str_pad($n, 2, '0', STR_PAD_LEFT); ?>"><? echo str_pad($n, 2, '0', STR_PAD_LEFT); ?></option>

appears 00 but doesn&#180;t insert the same to a mysql DB (only 0)

Can you help me?

Twey
05-16-2006, 10:42 AM
It's probably being converted to an integer at some point during the server-side processing. We'd need to see that.

pavmoxo
05-16-2006, 11:06 AM
The field is integer. Must be a string?

Twey
05-16-2006, 11:17 AM
Yes, any conversion to a number type will strip all leading zeroes (and trailing zeroes after the decimal point).

pavmoxo
05-16-2006, 11:31 AM
Thank's a lot for everything. You are very cool!!!!!