Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-16-2006, 07:01 AM   #1 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
Easy (for you ninjas) loop question

Hey guys,
really dumb question that I know has a simple answer...

I have this loop that generates a dropdown list for years. The problem is that the first current year it grabs initially is great (06) after it is incremented, I loose my "0" before the 6.. i.e...

Returns: 06,7,8,9
Instead of: 06,07,08,09

I'm sure there's a function or something that preserves the double, but can't seem to find it.

Here's the loop
Code:
<select name="expire_year">
<?
$curYear = date("y");
$yearCount = 1;

   echo "<option value='' SELECTED>- Select -</option>\n";

while ($yearCount < 10) {
   echo "<option value='".$curYear."'>".$curYear."</option>\n";

   $yearCount ++;
   $curYear ++;
}
?>
</select>
draven77 is offline   Reply With Quote
Old 02-16-2006, 07:19 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
personally I would use date("Y") so you have the 4 digit year. since they don't start with "0" you won't have this problem.

you could also create a string array with predefined dates:
PHP Code:
<?
$years 
= array("06","07","08","09","10");
foreach (
$years as $year) {
  echo 
"<option value='".$year."'>".$year."</option>\n";
}
?>
or str_pad if you want to keep using your way
PHP Code:
<?
$year 
date("y");

while(
$year 10) {
  
$year str_pad($year2"0"STR_PAD_LEFT);

  echo 
"<option value='".$year."'>".$year."</option>\n";

  
$year++;
}
?>
hth
sde is offline   Reply With Quote
Old 02-16-2006, 07:24 AM   #3 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
The reason i have to keep it like that is because it's getting passed to Authorize.net..

they require the expiration date to be sent in "mmyy".

So, i have 2 drop downs, one for month and one for year.
Then when I process it, i combine them.
$expire_date = $month.$year;

But it doesn't accept the expire date if for example it got May 08 as "508". It would have to be 0508.

I'm writing this loop so it isn't hard-coded and I don't have to update it next year. make sense ??

Thanks for the input!!
draven77 is offline   Reply With Quote
Old 02-16-2006, 07:42 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
yes makes sense. you might want to set your end year dynamically too then..

$end_year = $cur_year + 6;

then you won't have to update your end year next year as well.
sde is offline   Reply With Quote
Old 02-16-2006, 02:55 PM   #5 (permalink)
nudave04
Registered User
 
Join Date: Feb 2006
Posts: 1
nudave04 is on a distinguished road
Try this...

What about just appending the 0 as text when necessary? (Also, no need to increment a counter, just set an end point)

Code:
$curYear = floor(date("y"));
$endYear = $curYear + 10;

   echo "<option value='' SELECTED>- Select -</option>\n";

while ($curYear < $endYear) {
   if ($curYear < 10) {
        $displayYear = "0".$curYear;
        } else {
        $displayYear = $curYear;
    }
   echo "<option value='".$displayYear."'>".$displayYear."</option>\n";
   $curYear ++;
}
nudave04 is offline   Reply With Quote
Old 02-16-2006, 05:12 PM   #6 (permalink)
draven77
Recruit
 
Join Date: May 2005
Posts: 13
draven77 is on a distinguished road
Very nice, nudave04.. I like that option a lot.

simple, not overthought. great.
And I can understand it

Thanks guys! you all rulzor !
draven77 is offline   Reply With Quote
Old 02-16-2006, 05:24 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
That's cool that it all makes sense. Another cool thing is that
PHP Code:
$displayYear str_pad($year2"0"STR_PAD_LEFT); 
is equivalent to
PHP Code:
   if ($curYear 10) {
        
$displayYear "0".$curYear;
        } else {
        
$displayYear $curYear;
    } 
So now you can plug that into nudave04's loop and get
PHP Code:
while ($curYear $endYear) {
   
$displayYear str_pad($curYear2"0"STR_PAD_LEFT);
   echo 
"<option value='".$displayYear."'>".$displayYear."</option>\n";
   
$curYear ++;

__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 02-16-2006, 06:43 PM   #8 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
or
PHP Code:
echo "<option value='' selected='selected'>- Select -</option>\n";

$curYear = (int) date('y');
for (
$i=0$i 10; ++$i) {
   
$displayYear str_pad($curYear+$i2'0'STR_PAD_LEFT);
   echo 
"<option value='$displayYear'>$displayYear</option>\n";

DJMaze is offline   Reply With Quote
Old 02-16-2006, 07:05 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
i think i'll write a book .. 101 ways to code a year drop down in PHP
sde is offline   Reply With Quote
Old 02-16-2006, 10:48 PM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
Or more simplyfied:
PHP Code:
echo "<option value='' SELECTED>- Select -</option>\n";
for (
$i=floor(date("y")), $j=$i+10$i $j; ++$i)
   
printf("<option value='%.2d'>%.2d</option>\n"$i$i); 
But then again there are hundreds of ways you can form something like this.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy JavaScript question keystoneman HTML, XML, Javascript, AJAX 4 11-23-2004 05:29 AM
Easy Question Ilya020 Standard C, C++ 1 03-02-2003 05:31 PM
the endless loop loop loop loop loop loop ... abc123 All Other Coding Languages 2 08-14-2002 03:43 PM


All times are GMT -8. The time now is 09:28 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting