alright so I have this
<?php $xx = 01;
WHILE ($xx <= 31) {
echo "<option value='$xx'>$xx</option>";
$xx++;
} ?>
now the result is
1
2
3
4
etc
how do I get the results to be ?
01
02
03
04
etc
alright so I have this
<?php $xx = 01;
WHILE ($xx <= 31) {
echo "<option value='$xx'>$xx</option>";
$xx++;
} ?>
now the result is
1
2
3
4
etc
how do I get the results to be ?
01
02
03
04
etc
may I ask why exactly you want this.
use str_pad
http://ca2.php.net/manual/en/function.str-pad.php
i guess he wants it cause in the drop down box he wants the values to be 01 and not 1
reg
kevin
I'd firstly clean that up by using a for loop instead of a while loop. For loops were developed to do what you are using this while loop to do. I can't remember off of the top of my head if there is a function to add on leading zeros, so a simple if statement will do
for($i=1; $i <31; $i++) {
if(strlen($i) < 2) {
echo "0" . $i . "<br>";
} else {
echo $i . "<br>";
}
}
Cgraz
here u go
$xx = 01;
WHILE ($xx <= 31) {
$xx = str_pad($xx, 2, "0", STR_PAD_LEFT);
echo "<option value='$xx'>$xx</option>";
$xx++;
}
reg
kevin
Alright so to me the for and while loop are doing the same thing in this instance.
So what would be the difference?
Is For quicker then while? or vice versa?
Thanks for all the help
And yes I would like the leading zeros for a drop down as I think it looks better
If you want to add leading zeros you can use printf( '%02d',$xx );
As for the while() loops they are a poor mans for() loop.
HalfaBee
Use formatted print to string (sprintf) or formatted print (printf)
halfabee - why is that? what are the differences in them?
I just find it ridiculous to use 3 extra lines of code in a while loop when that is what a for() loop is designed for.
The for() construct tells you exactly what is happening in 1 line of code.
The while() construct makes you have to look through the whole loop just to work out it is counting from 0-9
Its easy when there is only 1 or 2 lines of code, but if you have 50 lines inside the loop its a pain.
HalfaBee
$i=0;
while( $i<10 )
{
echo $i;
$i++;
}
for( $i=0;$i<10;$i++ )
echo $i
its generally easier to use for loops and makes more sense when you know how many results you want or need, but when you have an unexpected results number, a while loop is the way to go, as in getting a results array, it is much easier to use a while loop than a for loop, IMHO
WHILE is most useful when you want to be able to control the increment amount inside the loop (such as skipping a value). Otherwise, I agree, FOR makes more sense, and I think most compilers do a better job of optimizing FOR statements than WHILE's with a loop increment.