Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts

    adding prev and next links + events

    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 Code:
    <?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>
    Last edited by rjkdonaldson; 12-08-2008 at 09:27 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Where do I put the code?

    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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?

    PHP Code:
    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>"
    Last edited by rjkdonaldson; 12-09-2008 at 05:56 AM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    In the links. The offset runs via the $_GET superglobal:
    PHP Code:
    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.
    PHP Code:
    $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>; 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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>;

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    My bad.
    PHP Code:
        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):
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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?

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Post you're new code and I'll take a looksee.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #12
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Current code

    Here's what I have so far, calendar wont display current month and shows current month as november instead.

    PHP Code:

    <?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();
    ?>
    Last edited by rjkdonaldson; 12-11-2008 at 11:31 AM.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah, I see. I didn't think that one through. I'll take a look after.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

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

    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 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    rjkdonaldson (12-12-2008)

  • #15
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •