next three months

next three months

Postby siggy » Thu Apr 03, 2008 6:05 am

I love the display of a three month period, but would love
some type of user control on the web page that allowed the user
to jump forward to the next 3 months or backward to the previous 3 months...
- or advance one month - while still showing a 3-month period

much thanks for all you great work
siggy
siggy
 
Posts: 13
Joined: Fri Mar 28, 2008 5:46 am

Re: next three months

Postby chris » Thu Apr 03, 2008 11:19 am

Hi there siggy and welcome to the forum :)

I have come up with a solution to your particular request which I must point out to other readers may not suit all needs ;)

Could you first check out this url: http://www.cbolson.com/code/availabilit ... ersion2.0/ to see if it is waht you are looking for before I start explaining the code changes.

Although you are after just 3 month blocks, I actually decided that whilst I was working on this I would make it adaptable to all 12 month view options.
However, it gets a bit complicated with year changes so I might have missed something somewhere.

Let me know if this is more or less what you where looking for ;)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: next three months

Postby siggy » Thu Apr 03, 2008 8:23 pm

Chris

absolutely a perfect solution.....

-for my needs - I will likely hard-code it to 3 months
but that is easy

you are the greatest


much thanks
siggy
siggy
 
Posts: 13
Joined: Fri Mar 28, 2008 5:46 am

Re: next three months

Postby chris » Thu Apr 03, 2008 8:43 pm

Well, let's see if I can talk you through what I did and at the same time you can see how to hard code it (actually just changing the select box code will be enough)

Find this code:
Code: Select all
$the_months   =   array();
$k         =   $cur_month;
$j         =   0;

while($j<MONTHS_TO_SHOW){
   $this_month=sprintf("%02s",$k);
   $the_months[$this_month]=array("year"=>$cur_year,"month"=>$this_month);
   $j++;
   if($k==12){
      //   start new year and reset counter to 1 (jan)
      $cur_year=$cur_year+1;
      $k=1;
   }else{
      $k++;
   }
}

and replace the whole lot with this:
Code: Select all
$the_months   =   array();
//   define start month - request on forum by siggy
if(isset($_REQUEST["start_month"]))   $start_month = $_REQUEST["start_month"];
else                        $start_month = $cur_month;

$j   =   0;
$this_month=$start_month;
$this_year   =   $cur_year;#   define as current year
while($months_shown<MONTHS_TO_SHOW){
   //   catch posssible year changes
   if(substr($this_month,0,1)=="-"){
      $this_month   =   12-str_replace("-","",$this_month);   #   remove symbol from val and to math
      $this_year   =   $cur_year-1;                  #   remove 1 from current year
   }elseif($this_month>12){
      $this_month   =   $this_month-12;                  #   remove full 12 months from cal num
      $this_year   =   $cur_year+1;                  #   add 1 to current year
      ++$cur_year;                              #   add 1 to year counter
   }
   //echo "<br>".$this_month." : ".$this_month." - ".$this_year;
   $this_month=sprintf("%02s",$this_month);
   $the_months[$this_month]=array("year"=>$this_year,"month"=>$this_month);
   ++$months_shown;
   ++$this_month;
}


That is actually all the "hard" stuff, I have tried to capture the number of months and the starting month and detect if it changes year (either next or previous)
I have renamed a couple of variables so that hopefully it will be a bit easier to understand :)
You will notice that there is a new $_REQUEST variable being used, $_REQUEST["start_month"] the way I have coded this is so that the month to start from (number) is actually send by the choice in the select box.

Here is the code for the select box (note - for it to work correctly the select box needs to be defined AFTER the various $_REQUEST values are detected):
Code: Select all
<select name="start_month" onchange="this.form.submit()">
               <option value="'.$cur_month.'" ';                if($start_month==$cur_month)       echo ' selected'; echo '>current '.MONTHS_TO_SHOW.' month block</option>
               <option value="'.($cur_month-MONTHS_TO_SHOW).'" ';    if($start_month==($cur_month-MONTHS_TO_SHOW))    echo ' selected'; echo '>last '.MONTHS_TO_SHOW.' month block</option>
               <option value="'.($cur_month+MONTHS_TO_SHOW).'" ';    if($start_month==($cur_month+MONTHS_TO_SHOW))    echo ' selected'; echo '>next '.MONTHS_TO_SHOW.' month block</option>
               <option value="'.($cur_month+1).'" ';             if($start_month==($cur_month+1))    echo ' selected'; echo '>'.MONTHS_TO_SHOW.' block from next month</option>
            </select>

Clearly this doesn't have to be a select list but note, as I said, I have actually defined the months to show here in the value of "start_month".
In fact, in your case, by just not having the other select list where the user can define the number of months "should" be enough for it to work as you want.

I am sure that you will be able to adapt this to your needs. Just let me know if you have any problems or need further help ;)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: next three months

Postby siggy » Fri Apr 04, 2008 2:05 am

I will give this a GO.... ( a weekend project )

-- I will certainly let you know how it turns out


siggy
siggy
 
Posts: 13
Joined: Fri Mar 28, 2008 5:46 am

Re: next three months

Postby siggy » Fri Apr 04, 2008 5:55 am

OK --- small problem ( I think )

using YOUR example on the link posted..

when one selects "Next 3 month block"
then July-Aug-Sept is correctly displayed

then if one selects "Last 3 month block"
Jan-Feb-Mar is dispalyed...

rather than the expected: Apr-May-Jun block
siggy
 
Posts: 13
Joined: Fri Mar 28, 2008 5:46 am

Re: next three months

Postby siggy » Fri Apr 04, 2008 6:04 am

looking at your example link more closely

I guess it is not exactly what I had in mind..

this is what I am thinking
when opened -- the current 3-month block is displayed (Apr-May-Jun)
when one selects the option "Next 3 months"
then the months Jul-Aug-Sept -- are displayed
THEN when one selects (or clicks) the option "Next 3 months"
the months Oct-Nov-Dec are displayed...

-- going backwards in time would work the same way


in this way I can track/display - potentially "years" of
room availability as viewed in 3-month segments

siggy
siggy
 
Posts: 13
Joined: Fri Mar 28, 2008 5:46 am

Re: next three months

Postby chris » Fri Apr 04, 2008 11:25 am

OK, this actually makes more sense so I probably just miss-understood you ;)

Take another look at my demo: http://www.cbolson.com/code/availabilit ... ersion2.0/
and see if this is closer to what you need.

I have now removed the ability to select the number of months to show as this caused unexpected results when combined :(

I think that this is an interesting (an necessary ) development so I will probably include it in the next release (whenever that might be)

Let me know if this is is so that I can post the code.

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: next three months

Postby siggy » Fri Apr 04, 2008 7:58 pm

YES -- that is it......

I really do think others will find this enhancement very useful


siggy
siggy
 
Posts: 13
Joined: Fri Mar 28, 2008 5:46 am

Re: next three months

Postby chris » Fri Apr 04, 2008 8:46 pm

Yes, I think that this is definalty a good idea to add to the next release version. It is actually on my (unpublished) roadmap just wasn't planning on doing it yet. This being a free script relying on donations which I supposedly develop in my free can't get as much attention as I would like :(

Anyway, let's see if I can explain this....

Add this somewhere near the beginning of the file to catch the requested data or define defaults before we start drawing the calendar:
Code: Select all
if((isset($_REQUEST["year_month"])) && (!empty($_REQUEST["year_month"])) ){
   $date_bits=explode("-",$_REQUEST["year_month"]);   #   0=year, 1=month
   define("START_YEAR",    $date_bits[0]);
   define("START_MONTH",   $date_bits[1]);
}else{
   define("START_YEAR",   date('Y'));
   define("START_MONTH",   date('m'));
}


Next, find the bit of code that we modified yesterday that looped through the months to show (starting from "$the_months=array();" and ending BEFORE "// loop through months to draw calendar") and replace with this:
Code: Select all
$the_months   =   array();

$j   =   0;
$this_year   =   START_YEAR;
$this_month   =   START_MONTH;


while($num_months_shown<MONTHS_TO_SHOW){
   //   star new year and reset month numbers
   if($this_month>12){
      $this_month   =   $this_month-12;                  #   remove full 12 months from cal num
      $this_year   =   $this_year+1;                  #   add 1 to current year
   }
   //echo "<br>".$this_month." - ".$this_year;
   $this_month=sprintf("%02s",$this_month);            #   add 0 if less that 10
   $the_months[$this_month]=array("year"=>$this_year,"month"=>$this_month);
   ++$num_months_shown;
   ++$this_month;
}


Finally the code for the form (select, radio, hrefs or whatever - my version is with select lists)
This was actually the toughest bit of code to get right as this is where we define the starting month AND year, being careful to catch the year changes.
This may not be the best or simplest way to send tell the calendar what to show, but it works and was able to take into account the months that we are showing (even though you want static 3 month blocks) and also the possibility to actually display the month names in the next and previous blocks (though I have disactivated that for your purpose). Well, here goes....
Just before your "form" for changing the blocks and AFTER the calendar has been defined add this:
Code: Select all
//   define select list month options and texts

//   define LAST block - START
$last_month_start   =    (START_MONTH-MONTHS_TO_SHOW);   #   remove months_to_show x 2 to go back this block and then 1 more
$last_year_start   =   START_YEAR;
if($last_month_start<0){
   $last_month_start=$last_month_start+12;
   --$last_year_start;
}

//   define LAST block - END
$last_month_end   =   $last_month_start+MONTHS_TO_SHOW;
$last_year_end   =   $last_year_start;
if($last_month_end>11){
   $last_month_end=$last_month_end-12;
   ++$last_year_end;
}

//   define months for text
//$last_block_txt      =   $lang["months"][$last_month_start-1]." ".$last_year_start."  to ".$lang["months"][$last_month_end-2]." ".$last_year_end;
$last_block_txt      ="Last 3 months";

//   define NEXT block - START
$next_month_start   =    $this_month;   #   just use MONTH number that the loop has ended on
$next_year_start   =   $this_year;      #   just use YEAR number that the loop has ended on
if($next_month_start>11){
   $next_month_start=$next_month_start-12;
   ++$next_year_start;
}

//   define NEXT block - END
$next_month_end   =   $next_month_start+MONTHS_TO_SHOW;
$next_year_end   =   $next_year_start;
if($next_month_end>11){
   $next_month_end=$next_month_end-12;
   ++$next_year_end;
}

//   define months for text
//$next_block_txt      =   $lang["months"][$next_month_start-1]." ".$next_year_start."  to ".$lang["months"][$next_month_end-1]." ".$next_year_end;
$next_block_txt=   "Next 3 months";

Finally you will need to add the elements to the actual form which, as I said, is a select list in my case:
Code: Select all
<select name="year_month" onchange="this.form.submit()">
               <option value=""> - select months to show - </option>
               <option value=""> Current '.MONTHS_TO_SHOW.' months </option>
               <option value="'.$last_year_start.'-'.$last_month_start.'">'.$last_block_txt.'</option>
               <option value="'.$next_year_start.'-'.$next_month_start.'" >'.$next_block_txt.'</option>
            </select>

And that is it ;)

I hope you are able to follow these instructions and also that I haven't missed any code out, I did this several hours ago and my mind has since moved on (if not my body)
Let me know how it goes ;)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Next

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests

cron