Jump to content

Recommended Posts

Hi, I'd really appreciate some guidance regarding the GET method. 

 

Thus far I've developed a calendar that displays a month at a time.

I've created a 'next' button (div) that onclick - it submits values via GET.  This works well.

 

<div onclick="location.href='cal.php?m=<?php if ($monthly == 12) { 
$monthly = 1; $yearly++; } else { $monthly++; } 
echo $monthly?>&y=<?php echo $yearly?>'" id="nextMonth" class="nextMonth next"></div>

 

The complication comes with the 'prev' button, which I thought would be the reverse of the next button (logic wise).

 

<div onclick="location.href='cal.php?m=<?php if ($monthly == 1) {
$monthly = 12; $yearly--; } else { $monthly--; } 
echo $monthly?>&y=<?php echo $yearly?>'" id="prevMonth" class="nextMonth prev"></div>

 

Can you see any reason why this 'prev' button does not work?

 

FYI, This reads GET and initiates the page

$m = !empty($_GET["m"]) ? $_GET["m"] : '';
$y = !empty($_GET["y"]) ? $_GET["y"] : '';
if ($m) {
   $monthly = ($m);
   $yearly = ($y);
} else { 
$now = time(); 
$cur_month = date("n", $now);
$cur_year = date("Y", $now);
$monthly = $cur_month; 
$yearly = $cur_year; }

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/221868-unhappy-calendar-get/
Share on other sites

Thanks for the replies, things are happening here.  Whether positive/negative I'm not too sure.

 

I've altered the 'prev' button to:

 

if ($monthly == 1) { $monthly = 12; $yearly=$yearly-1; } else { $monthly=$monthly-2; }

 

Which now changes the month successfully. However, when in January, it correctly takes it back to December of the previous year, but it sticks at this point.  Where I'm expecting it to again shuffle down the months, 11, 10, 9, 8 etc.

 

I don't have much confidence in the fact that its $monthly-2.. strange.

 

I think you are getting mixed up by the value of $monthly. 

 

Start with the calendar for, say May (month = 5).

If you output the DIV for next first, your code is incrementing $monthly. So now it is month 6 (June).

 

If you THEN output the DIV for previous, your code is operating on the NEW value (6), so you have to subtract 2 from it to get the month previous to the current one.

 

Since the code for previous is seeing a $monthly value that is one more than the current month (or it may even be the value 1) your if ($monthly == 1) is not correct. It should actually be if ($monthly == 2).

 

It is confusing. If you make that change it should work, but the next time you read through the code to make changes, you will wonder why you did it that way (trust me, we've all been there).

 

The code will be more clear if you actually add a couple of variables and calculations before generating the DIV's:

 

// untested code off the top of my head
$nextMonth = $monthly + 1;
if ($nextMonth > 12) {
  $nextMonth = 1;
  $nextYear = $yearly + 1;
} else {
  $nextYear = $yearly;
}

$prevMonth = $monthly - 1;
if ($prevMonth < 1) {
  $prevMonth = 12;
  $prevYear = $yearly - 1;
} else {
  $prevYear = $yearly;
}

Or something along those lines. Then output $nextMonth and $prevMonth for the DIV events.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.