View Full Version : range() function
ClubCosmic
09-04-2004, 02:19 PM
i created a select box in a form that i want the minutes listed. eg: 10:01 i used range(00, 59);
it works but the drop list has 0, 1, 2, etc instead of 00, 01, 02
is there a way to display it that way without making a select box with 60 items typed out in it?
thanks
trib4lmaniac
09-04-2004, 02:29 PM
Before echoing the numbers run this...
foreach($range_array as $key=>$number)
if($number<10)
$range_array[$key]='0'.$number;
$range_array is the array returned by range()
ClubCosmic
09-04-2004, 02:41 PM
thank you,
i didn't know about the $range_array
i have learned a lot from my beginners book, are there any good books you know of tht might be good for me that are alittle more advanced?
trib4lmaniac
09-04-2004, 02:49 PM
No no no, theres nothing special about the word $range_array! It's just a variable.
When you call the function range(0, 59), that function returns an array of all the numbers from 0 to 59, ie...
$my_variable_that_can_be_called_anything = range(0, 59);
Now "$my_variable_that_can_be_called_anything" contains all the numbers from 0 to 59 in an array format. I just chose the name $range_array at random.
As for books, I just taught myself from the net.
marek_mar
09-04-2004, 04:05 PM
Why iterate over the whole array when we know that we need to prepend a '0' only to the first 10?
<?
$range_array = range(0, 59);
for($i=0; $i<10; $i++)
{
$range_array[$i]='0'.$range_array[$i];
}
?>
This is faster but less flexible.
trib4lmaniac
09-04-2004, 04:08 PM
What if one day he decides to print them out backwards with range(59, 0)?
You don't lose much time, but you get far more flexibility.
marek_mar
09-04-2004, 04:30 PM
Didn't I mention that?
trib4lmaniac
09-04-2004, 04:31 PM
You did, but I think the first way is better. You, on the other hand, seem to differ.
No fuss though :cool:
firepages
09-04-2004, 04:58 PM
you can also simply use str_pad() (http://www.php.net/str_pad) or number_format() (http://www.php.net/number_format) wherever you print out the value or in the iteration , lets not get stuck on which is `best`
marek_mar
09-04-2004, 05:13 PM
Well I gave an ALTERNATIVE. Both have pros and cons. Why do you think I differ. He might as well do a range of something else which has less than 10 and there should be no 0 prepended (well this is kind of stupid argument -> he would'nt have to anything with the array) or more than 100 where 00 should be prepended as well as 0.
that was supposed to go before firepages post
firepages
09-04-2004, 05:30 PM
I am not arguing with anyones input , & I have no problems if 10 alternative answers are given , in fact the more the merrier , just wanted to note that there have been a few drawn out threads recently where the point has been lost, with the danger perhaps of those who asked the original question `not being able to see the wood for the trees ` as it were.
I dont want that happening here is all :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.