3

Wondering how I would use either EE or PHP to dynamically generate a list of years starting from this year. I'm doing this for a credit card expiry select box that needed to use EE global variables for the label, which is why i couldn't just use the fields spit back from the gateway. And of course their example code is just a static list starting with 2012, so I'd rather the list be dynamic for say 10 years starting this year. Can someone tell me how to do this?

3

Assuming PHP is enabled in your template, this should get you started...

<select name="credit-card-expiration-year" id="credit-card-expiration-year">
  <?php
   for($i = date("Y"); $i < date("Y")+10; $i++){
     echo "<option>" . $i . "</option>";
   }
  ?>
</select>
1
  • I certain CAN enable PHP in the template. Is that the most effective way to handle it? I didn't figure there was an easy way to do it with EE tags. Dec 7 '12 at 21:15
7

I wrote a simple plugin to do this and you can also use this plugin (I did not write rangee): http://devot-ee.com/add-ons/rangee

2
  • Awesome. I didn't know about rangee - have bookmarked for future projects.
    – CreateSean
    Dec 7 '12 at 21:33
  • Very cool - I hadn't known about rangee either Dec 7 '12 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.