i have the following code to create a select element of numbers from 0-99 using php range. I've tried to add the 00 to the range like this with no luck: range(00, 99) but it still just lists a single digit like 2 instead of 02. How do I alter this?
<select name="Vehic" id="Vehic" >
<option value="">Truck</option>
<?php
foreach (range(0, 99) as $trucknum) { ?>
<option value="<?php echo($trucknum); ?>"><?php echo($trucknum); ?></option>
<?php } ?>
</select>
在输出上。
<? printf('%02d', $trucknum); ?>
Use str_pad() :
echo str_pad($trucknum, 2, '0', STR_PAD_LEFT);
In your code:
<select name="Vehic" id="Vehic" >
<option value="">Truck</option>
<?php
foreach (range(0, 99) as $trucknum) { ?>
<?php
$trucknumStr = str_pad($trucknum, 2, '0', STR_PAD_LEFT);
?>
<option value="<?php echo($trucknum); ?>"><?php echo($trucknumStr); ?></option>
<?php } ?>
</select>
$trucknum is a numeric value, you will have to format the output to add leading zeroes when needed. See http://php.net/manual/en/function.printf.php
this worked <?php echo sprintf('%02d', $i); ?>
<?php echo sprintf('%02d', $i); ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.