0

I have found a couple of loop plugins but I cant quite figure out how to get what I'm looking for.

I would like to have a select dropdown in my template that looks like this:

October   2014
November  2014
December  2014
January   2015
February  2015

How would I accomplish this?

0

You could try combining the DT Plugin with the Loop Plugin.

This is what your code would look like:

<select id="expiry1">
  {exp:for_loop start="0" end="12" increment="1" parse="inward"}
    <option value="{exp:dt format='%U' month='{index}'}">
      {exp:dt format='%M %Y' month='{index}'}
    </option>
  {/exp:for_loop}
</select>

To explain how this works:

The start= and end= parameters allow us to sequentially loop from a start-end iteration.

We've set the loop to start from 0 which means the first iteration will bemonth="0". This comes in handy as the DT Plugin (or most plugins for that matter) disregard zero-value parameters and therefore evaluates it as if the month parameter were not even provided.

Note: We are also providing the parse="inward" parameter to the outer plugin to allow the {index} variable to parse before DT Plugin accepts it as a parameter!

Hope this helps!

  • Thanks Cue, this sounds like a great solution. I'll try it asap and report back. :) – Louis K Jul 26 '14 at 19:22
  • It works like a charm!! Thank you very much! – Louis K Jul 26 '14 at 21:08
0

I created use the plugin stringy for a lot of things and I added this function to it today because I needed the same thing. This could probably be trimmed down but it works. You can pass in the selected date in the selected parameter, I just pass in a specific segment. If not selected parameter is passed in it uses the current year and month as the selected date. You might have to change the date formatting to meet yours needs. This by the way ONLY outputs the option tags, you need to put this inside the tag.

Here is the code.

public function month_select()
{
    $sel_date = $this->EE->TMPL->fetch_param('selected');

    if ($sel_date == "") {
        $sel_date = date("Y-m-d");
    }

    $ret = "";
    $date = date('Y-m-d');
    $month = date("n", strtotime($date));
    $year = date('Y');

    for ($cm=0; $cm<12; $cm++) {
        $is_sel = false;
        $sel_str = "";

        $tm = ($month + $cm);
        $ty = $year;

        if ($tm > 12) {
            $tm = $tm - 12;
            $ty = $year + 1;
        }

        $ds = date("F Y", strtotime($ty . "/" . $tm . "/1"));

        if ($ty == date("Y", strtotime($sel_date)) && $tm == date("n", strtotime($sel_date))) {
            $is_sel = true;
            $sel_str = 'selected="selected"';
        }

        $ret .= '<option value="' . $ty . '-' . $tm . '" ' . $sel_str . '>' . $ds . '</option>';
    }

    return $ret;
}
0

Thanks a lot Trevor Orr! I have managed to accomplish what I wanted to do with a plugin called DT (http://viminteractive.com/software/dt_plugin).

Because I only needed a couple of loop itirations I hardcoded the select options and offsetted the dates like this:

<select id="expiry1">
     <option value="{exp:dt format='%U'}">{exp:dt format='%M %Y'}</option>
     <option value="{exp:dt format='%U' month='1'}">{exp:dt format='%M %Y' month='1'}</option>
     <option value="{exp:dt format='%U' month='2'}">{exp:dt format='%M %Y' month='2'}</option>
     <option value="{exp:dt format='%U' month='3'}">{exp:dt format='%M %Y' month='3'}</option>
     <option value="{exp:dt format='%U' month='4'}">{exp:dt format='%M %Y' month='4'}</option>
     <option value="{exp:dt format='%U' month='5'}">{exp:dt format='%M %Y' month='5'}</option>
     <option value="{exp:dt format='%U' month='6'}">{exp:dt format='%M %Y' month='6'}</option>
     <option value="{exp:dt format='%U' month='7'}">{exp:dt format='%M %Y' month='7'}</option>
     <option value="{exp:dt format='%U' month='8'}">{exp:dt format='%M %Y' month='8'}</option>
     <option value="{exp:dt format='%U' month='9'}">{exp:dt format='%M %Y' month='9'}</option>
     <option value="{exp:dt format='%U' month='10'}">{exp:dt format='%M %Y' month='10'}</option>
     <option value="{exp:dt format='%U' month='11'}">{exp:dt format='%M %Y' month='11'}</option>
     <option value="{exp:dt format='%U' month='12'}">{exp:dt format='%M %Y' month='12'}</option>
</select>

The first option is always the current month and year and for the following 12 months 1 month is added to the current. It would be nice to be able to make this an actual loop.

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.