...

View Full Version : adding prev and next links + events



rjkdonaldson
12-08-2008, 09:25 AM
Was wondering how to add next and previous links to this calendar. Trying to get the calendar to function in that it has previous and next links to cycle through the months. How do I go about adding events to it? Below is the code I have so far.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>


<?php
function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the actual day
$today = getdate();
$firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


// Create a table with the necessary header informations
echo '<table>';
echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
echo '<tr class="days">';
echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


// Display the first calendar row with correct positioning
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
echo '<td>&nbsp;</td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($i==6)or($i==7)) $class .= ' style="background-color:#3399ff"';
echo "<td$class>$actday</td>";
}
echo '</tr>';

//Get how many complete weeks are in the actual month
$fullWeeks = floor(($lastDay['mday']-$actday)/7);

for ($i=0;$i<$fullWeeks;$i++){
echo '<tr>';
for ($j=0;$j<7;$j++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($j==5)or($j==6)) $class .= ' style="background-color:#3399ff"';
echo "<td$class>$actday</td>";
}
echo '</tr>';
}

//Now display the rest of the month
if ($actday < $lastDay['mday']){
echo '<tr>';

for ($i=0; $i<7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}

if (($i==5)or($i==6)) $class .= ' style="background-color:#3399ff"';
if ($actday <= $lastDay['mday']){
echo "<td$class>$actday</td>";
}
else {
echo '<td>&nbsp;</td>';
}


}


echo '</tr>';
}

echo '</table>';
}

showCalendar();
?>

</body>
</html>

Fou-Lu
12-08-2008, 11:50 AM
Ok, this may be kinda hard to explain.
I'm not going to go over most of this code though, you're first few lines are enough.

Essentially, you'd perform an actual similar to the use of a record pagination. For example, we know what month it currently is, so we can detect the next month and the previous month (and the years, though it should actually account for year changes - the timezone offset).
So, instead of just using getdate, you'd go like so:
http://www.mysite.com/calendar.php?monthOffset=-4


function showCalendar()
{
$monthOffset = isset($_GET['monthOffset']) ? $_GET['monthOffset'] : 0;
$curDate = strtotime($monthOffset . " month");
$today = getDate($curDate);


Simple as that. Later you can generate some forward and back links based around the $curDate value, since that should contain the month info based on the generated month, not the current.

PHP has probably the best date / time handling I've seen of any language. Strtotime isn't a PHP concept, but its awesome nonetheless.

rjkdonaldson
12-09-2008, 04:06 AM
Can I place the chunk of code you gave me and just change the variables? More insight would be good. I really need this calendar to function with all the bells and whistles as I am creating a website voluntarily for a non-profit organization.

Fou-Lu
12-09-2008, 04:19 AM
Give it a shot and test it out. Like I said, I didn't go through all of you're code, but since you're basing everything off of getdate all you need to do is alter the value of getdate to generate the new values.
I'm pretty sure that years are accommodated with strtotime when months > 12. So if you add +16 months, I'm pretty sure it knows to add 1 year and 4 months.
The only variable you'd need to change is the handling for the monthOffset. With my example, it takes an integer result to reprsent +/- months. Everything else you can just leave and replace the value of $today with the today that I've used (exactly the same except with a timestamp).

Clear as mud? I've been awake for 22 hours now, but it sounds right in my head lol.

rjkdonaldson
12-09-2008, 06:50 AM
This is what I have so far the calendar has links at top near the month and year. Could you explain where the php code goes to make the previous and next links function correctly?



function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the actual day
$monthOffset = isset($_GET['monthOffset']) ? $_GET['monthOffset'] : 0;
$curDate = strtotime($monthOffset . " month");
$today = getDate($curDate);
//$today = getdate();
$firstDay = getDate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getDate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


// Create a table with the necessary header informations
echo '<table>';
echo ' <tr><th colspan="7"> <a href="#" class="date">prev</a> '.$today

['month']." - ".$today['year']." <a href='#' class='date'>next</a> </th></tr>";

Fou-Lu
12-09-2008, 01:30 PM
In the links. The offset runs via the $_GET superglobal:


echo '<table>';
echo ' <tr><th colspan="7"> <a href="#" class="date">prev</a> '.$today

['month']." - ".$today['year']." <a href='#' class='date'>next</a> </th></tr>";

Add into you're href. There are plenty of ways to do this, but we'll just use the querystring without any other information.


$sNegMonth = urlencode('-1');
$sPosMonth = urlencode('+1');
echo ' <tr><th colspan="7"> <a href="?monthOffset=' . $sNegMonth . '" class="date">prev</a> '.$today

['month'].' - '.$today['year'].' <a href='?monthOffset=" . $sPosMonth . '" class="date">next</a> </th></tr>';

rjkdonaldson
12-09-2008, 08:06 PM
Is this the correct way of doing it? I now get this error:
Parse error: syntax error, unexpected '=' in C:\inetpub\wwwroot\tdict_website\calendar2.php on line 28.
Can you see where I've gone wrong



<?php
function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the

actual day
$monthOffset = isset($_GET['monthOffset']) ? $_GET

['monthOffset'] : 0;
$curDate = strtotime($monthOffset . " month");

$today = getDate($curDate);
//$today = getdate();
$firstDay = getDate(mktime(0,0,0,$today

['mon'],1,$today['year']));
$lastDay = getDate(mktime(0,0,0,$today['mon']

+1,0,$today['year']));
$sNegMonth = urlencode('-1');
$sPosMonth = urlencode('+1');


// Create a table with the necessary header

informations
echo '<table>';
echo ' <tr><th colspan="7"><a href="?monthOffset='

. $sNegMonth . ' " class="date">prev</a> '.$today

['month'].' - '.$today['year'].' <a href='?monthOffset=" .

$sPosMonth . ' " class="date">next</a> </th></tr>;

Fou-Lu
12-10-2008, 02:22 AM
My bad.


echo '<table>';
echo ' <tr><th colspan="7"><a href="?monthOffset='

. $sNegMonth . '" class="date">prev</a> '.$today

['month'].' - '.$today['year']. ' <a href="?monthOffset=' .

$sPosMonth . '" class="date">next</a> </th></tr>';

As for you're actual error, I'm pretty certain its from this:
' <a href='?monthOffset=" .
Since my quotations were incorrect, its interpreting the phrase monthOffset as an attempted variable (which it cannot do), and whining about having a syntax error since it doesn't know how to assign a value to a non-variable.
Change that block to the code above, and it should work. I knew something looked off when I posted before.
I'm a print formatter, so I do mine differently (most of the time):


print("<table>");
printf("<tr><th colspan=\"7\"><a href=\"?monthOffset=%s\" class=\"date\"....", $sNegMonth, ....);

Something from using C.

I hope you still have the code you used to actually create the calendar though (with the loops and whatnots), since this doesn't actually display it, it only allows you to capture a different date then the current.

rjkdonaldson
12-10-2008, 05:00 AM
Hey Fou thanks for the tips man! Haven't tried it out yet though. Luckily I do still have the orginal code on my computer. I do most of my web dev locally using IIS 7 and PHP5. Never had to deal with C only JAVA nonetheless you are way ahead of me in terms of programming dude.

rjkdonaldson
12-10-2008, 05:51 AM
Hi Fou just got home and tried out the code you provided, it works now with the previous and next links but seems to default at november 2008 for some reason and jumps from november 2008 to january 2009 after clicking the next link. Any ideas?

Fou-Lu
12-10-2008, 01:23 PM
Post you're new code and I'll take a looksee.

rjkdonaldson
12-11-2008, 12:29 PM
Here's what I have so far, calendar wont display current month and shows current month as november instead.




<?php
function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the actual day
$monthOffset = isset($_GET['monthOffset']) ? $_GET['monthOffset'] : 0;
$curDate = strtotime($monthOffset . " month");

$today = getDate($curDate);
//$today = getdate();
$firstDay = getDate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getDate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
$sNegMonth = urlencode('-1');
$sPosMonth = urlencode('+1');


// Create a table with the necessary header informations
echo '<table>';
echo ' <tr><th colspan="7"> <a href="?monthOffset=
' . $sNegMonth . '" class="date">prev</a> '.$today

['month'].' - '.$today['year'].' <a href="?monthOffset=' . $sPosMonth . '" class="date">next</a> </th></tr>';

echo '<tr class="days">';
echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


// Display the first calendar row with correct positioning
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
echo '<td>&nbsp;</td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($i==6)or($i==7)) $class .= ' style="background-color:#3399ff"';
echo "<td$class>$actday</td>";
}
echo '</tr>';

//Get how many complete weeks are in the actual month
$fullWeeks = floor(($lastDay['mday']-$actday)/7);

for ($i=0;$i<$fullWeeks;$i++){
echo '<tr>';
for ($j=0;$j<7;$j++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}
if (($j==5)or($j==6)) $class .= ' style="background-color:#3399ff"';
echo "<td$class>$actday</td>";
}
echo '</tr>';
}

//Now display the rest of the month
if ($actday < $lastDay['mday']){
echo '<tr>';

for ($i=0; $i<7;$i++){
$actday++;
if ($actday == $today['mday']) {
$class = ' class="actday"';
} else {
$class = '';
}

if (($i==5)or($i==6)) $class .= ' style="background-color:#3399ff"';
if ($actday <= $lastDay['mday']){
echo "<td$class>$actday</td>";
}
else {
echo '<td>&nbsp;</td>';
}


}


echo '</tr>';
}

echo '</table>';
}

showCalendar();
?>

Fou-Lu
12-11-2008, 01:28 PM
Yeah, I see. I didn't think that one through. I'll take a look after.

Fou-Lu
12-12-2008, 02:35 AM
Here you go. Just changed the idea to using a simple timestamp.
I didn't look at you're looping code, but there's a problem with it. Check February 2009. Anytime the month begins on a Sunday, it will contain eight days that week.


Lol, guess I should post some code :P
Just the start, replace it to the comment you see:



define('TIMEVAR', 't');

function showCalendar(){
// Get key day informations.
// We need the first and last day of the month and the actual day
$useDate = getDate();
if (isset($_GET[TIMEVAR]) && is_numeric($_GET[TIMEVAR]))
{
$checkDate = getDate((int)$_GET[TIMEVAR]);

if ($checkDate['mon'] != $useDate['mon'] ||
($checkDate['mon'] == $useDate['mon'] &&
$checkDate['year'] != $useDate['year']))
{
$useDate = $checkDate;
}
}

$today = $useDate;
//$today = getdate();
$firstDay = getDate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getDate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
$iLastMonth = strtotime('-1 month', $useDate[0]);
$iNextMonth = strtotime('+1 month', $useDate[0]);


// Create a table with the necessary header informations
echo '<table>';
echo ' <tr><th colspan="7"> <a href="?' . TIMEVAR . '=' .
$iLastMonth . '" class="date">prev</a> '.$today['month'].
' - '.$today['year'].' <a href="?' . TIMEVAR . '=' .
$iNextMonth . '" class="date">next</a> </th></tr>';

echo '<tr class="days">';
echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


// Display the first calendar row with correct positioning

rjkdonaldson
12-12-2008, 07:52 AM
Code works, thanks alot I will have to look at the looping code as you suggested coz the dates fall out of place if you know what I mean. I got 2 of the same calendars just in case i mess up, one working one and one for modding. Thanks for the help.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum