Hi all,

I am a bit of a newbie to PHP but trying never less. I am using the following code, which uses a for loop to get the latest month and then display 10 (just a random number) previous months below it inside a drop down list.

The problem I am having is that instead of displaying October 2007 --> December 2006 I want the drop down list to start from the current month (October 2007 - obviously) then below it have September 2007, which is when my data starts from. September 2007 will always be the starting month, so as time goes on, say in January 2008 I need my drop down list to display:

January 2008
December 2007
November 2007
October 2007
September 2007

Hope I explained this problem ok.

Here is my code:

                                                                     echo 'Filter by month:';
										echo '<select name="month">';
										echo '<option value="?month=">All months</option>';
										$beginning_month = 10;

									for($x=0;$x<$beginning_month;$x++){

										$time = strtotime("$x months ago");
										$month = date("F Y",$time);
										$link = strtolower(date("MY",$time));

										if(isset($_GET['month']) && $link == $_GET['month']) {

											echo '<option value="'.$link.'" selected="selected">'.$month.'</month>';

										} else {
										echo '<option value="'.$link.'">'.$month.'</month>';
										}
									}

									echo '</select>';
									echo '<input type="submit" value="Filter" />';

Can anyone please help?

    You can suit this to your own code, but here is the basic idea to do what you want to do:

    for ($i=0; $i<10; $i++)
    {
        $timestamp = mktime(0,0,1,date("m")+$i,date("d"),date("Y"));
        $month = date("F Y", $timestamp);
        echo "{$month}<br />\n";
    }
    

      Thanks for the reply chrome29.

      However I'm not sure if you get what I need to do. Or I am lost.

      I need to start my list from the current month (October 2007) going back down to September 2007. So at the moment the drop down list will be:

      October 2007
      September 2007

      the month after it will need to be:

      November 2007
      October 2007
      September 2007

      etc. etc. etc.

      Hope i have explained myself better?!

      any ideas?

        Sorry, I should read better. Maybe this would do the trick:

        while ($month!="September 2007")
        {
            $timestamp = mktime(0,0,1,date("m")-$c,1,date("Y"));
            $month = date("F Y", $timestamp);
            echo "{$month}<br />\n";
            $c++;
        }
        

          Thanks a lot chrome29, code does just about what I want it to do 😃

          I am just trying to add some of my old codes features to it. I have edited it:

          		echo 'Filter by month:';
          		echo '<select name="month" style="margin:0 0 0 4px;" class="medium">';
          		echo '<option value="?month=">All months</option>';
          
          	while ($month!="September 2007")
          	{
          		$timestamp = mktime(0,0,1,date("m")-$c,1,date("Y"));
          		$month = date("F Y", $timestamp);
          		echo '<option value="'.$month.'">'.$month.'</month>';
          		$c++;
          	} 
          
          	echo '</select>';
          	echo '<input type="submit" value="Filter" class="submit" />';

          I was wondering if you could help me trying to get the values in the options to have the first 3 letters of the month then followed by the year?

            Sorry was being stupid.

            got it:

            		while ($month!="September 2007")
            		{
            			$timestamp = mktime(0,0,1,date("m")-$c,1,date("Y"));
            			$month = date("F Y", $timestamp);
            			$link = strtolower(date("MY", $timestamp));
            			echo '<option value="'.$link.'">'.$month.'</month>';
            			$c++;
            		} 

              Hey,

              Sorry but its me again. For some reason my loop is never ending when I put my if statement inside it. I am trying to determine which of the drop down options was selected.

              I have edited the code you have given me:

              		while ($month!="September 2007")
              		{
              			$timestamp = mktime(0,0,1,date("m")-$c,1,date("Y"));
              			$month = date("F Y", $timestamp);
              			$link = strtolower(date("MY", $timestamp));
              
              		if(isset($_GET['month']) && $link == $_GET['month']) {
              			// selected menu option equals abbreviated month
              			echo '<option value="'.$link.'" selected="selected">'.$month.'</month>';
              		// if not return the other months as normal	
              		} else {
              
              		echo '<option value="'.$link.'">'.$month.'</month>';
              		$c++;
              
              		}
              	} 

              Any suggestions?

                Move the $c++ line to between the second last } and the last } and you are all set.

                while ($month!="September 2007")
                {
                    $timestamp = mktime(0,0,1,date("m")-$c,1,date("Y"));
                    $month = date("F Y", $timestamp);
                    $link = strtolower(date("MY", $timestamp));
                
                if(isset($_GET['month']) && $link == $_GET['month']) 
                {
                    // selected menu option equals abbreviated month
                    echo '<option value="'.$link.'" selected="selected">'.$month.'</month>';
                } else {
                    // if not return the other months as normal    
                    echo '<option value="'.$link.'">'.$month.'</month>';
                }
                $c++;
                } 
                

                  thanks a lot mate, works perfectly

                    This wont change
                    the function of chrome29 code,
                    but I once had a similar drop-down script.

                    This is how I did it:

                    <?php
                    
                    $selected = " selected=\"selected\"";
                    
                    while ($month!="September 2007")
                    {
                        $timestamp = mktime(0,0,1,date("m")-$c,1,date("Y"));
                        $month = date("F Y", $timestamp);
                        $link = strtolower(date("MY", $timestamp));
                    
                    $sel = "";           
                    if(isset($_GET['month']) && $link == $_GET['month']) {
                        $sel = $selected;
                    }
                    
                    echo '<option value="' .$link. '"' .$sel. '>' .$month. '</month>';
                    $c++;
                    } 
                    
                      Write a Reply...