Jump to content

calendar


jinu227

Recommended Posts

hi,

 

i have implemented a calendar in php and added contols to display the previous and next months. How will I enhance my code to add links to display the previous and next year calendars?

 

My code is as follows:

<?php
session_start();
$_SESSION['username'];
$_SESSION['unique_id'];
$username = $_SESSION['username'];
$unique_id = $_SESSION['unique_id'];
?>

<?php
    $host = "localhost";
    $user=""; 
    $password=""; 
    $db_name=""; 
    $tbl_name=""; 
    
    $con = mysql_connect("localhost","","");
    if (!$con)
      {
          die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("$db_name", $con);
    
    // Now we need to define "A DAY", which will be used later in the script:
    define("ADAY", (60*60*24));
     
    // The rest of the script will stay the same until about line 82
     
    if ((!isset($_GET['month'])) || (!isset($_GET['year']))) {
        $nowArray = getdate();
        $month = $nowArray['mon'];  //mon - Numeric representation of a month
    $year = $nowArray['year'];
    } else {
        $month = $_GET['month'];
    $year = $_GET['year'];
    }
    $start = mktime(12,0,0,$month,1,$year);
    $firstDayArray = getdate($start);
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo "Calendar: ".$firstDayArray['month']."" . $firstDayArray['year']; ?></title>
<link type="text/css" href="css/style_inner.css" rel="stylesheet" />
</head>

<body>
    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="padding-top:50px">
    <?php
        /* "next month" control */
$next_month = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control" style="text-decoration:none; padding-left:50px;">Next Month  ></a>';

/* "previous month" control */
$previous_month = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control" style="text-decoration:none; padding-left:200px; padding-right:50px;"><  Previous Month</a>';


/*$next_year = '<a href="?year='.($year != 2050 ? $year + 1 : 1).'&year='.($year != 2050 ? $year : $year + 1).'" class="control" style="text-decoration:none; padding-left:10px;">Next Year  >></a>';

$previous_year = '<a href="?month='.($year != 2011 ? $year - 1 : 2050).'&year='.($year != 2011 ? $year : $year - 1).'" class="control" style="text-decoration:none; padding-left:150px;"><<  Previous Year</a>';

echo $previous_year;*/
echo $previous_month;
    ?>
    <select name="month"><br/>
    <?php
    $months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
     
    for ($x=1; $x<=count($months); $x++){
        echo "<option value=\"$x\"";
    if ($x == $month){
            echo " selected";
        }
        echo ">".$months[$x-1]."</option>";
    }
    ?>
    </select>
    <select name="year">
    <?php
    for ($x=2011; $x<=2050; $x++){
    echo "<option";
        if ($x == $year){
        echo " selected";
        }
        echo ">$x</option>";
    }
    ?>
    </select>
<input type="submit" name="submit" value="Go!">
<?php
    echo $next_month;
    /*echo $next_year;*/
?>
    </form>
    <br />
    <?php
    $days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    echo "<table border=\"1\" style=\"padding-left:100px;\"><tr>\n";
    foreach ($days as $day) {
        echo "<td style=\"background-color: #000000; width: 100px\" align=\"center\">
              <strong style=\"color:#FFFFFF;\">$day</strong></td>\n";
    }
     
    for ($count=0; $count < (6*7); $count++) {
        $dayArray = getdate($start);
        if (($count % 7) == 0) {
        if ($dayArray["mon"] != $month) {
            break;
        } else {
                echo "</tr><tr>\n";
        }
        }
        if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) { //wday - Numeric representation of the day of the week
        echo "<td height=\"110px\"> </td>\n";
        } else { //mday - Numeric representation of the day of the month
            $chkEvent_sql = "SELECT event_title, event_shortdesc FROM calendar_events WHERE month(event_start) = '".$month."' AND dayofmonth(event_start) = '".$dayArray["mday"]."' AND year(event_start) = '".$year."' ORDER BY event_start";
       $chkEvent_res = mysql_query($chkEvent_sql, $con) or die(mysql_error($con));

        if (mysql_num_rows($chkEvent_res) > 0) {
            $event_title = "<br/>";
            while ($ev = mysql_fetch_array($chkEvent_res)) {
                    $event_title .= "<font color=\"#006600\">" . stripslashes($ev["event_title"])."</font><br/>";
                    //$event_shortdesc .= stripslashes($ev["event_shortdesc"])."<br/>";
            }
                mysql_free_result($chkEvent_res);
            } else {
            $event_title = "";
            $event_shortdesc = "";
            }

        echo "<td height=\"110px\" style=\"color : #0000CC;\">".$dayArray["mday"]."<a href=\"event.php?m=".$month."&d=".$dayArray["mday"]."&y=".$year."\" style=\"text-decoration:none;\"><img src=\"images/Add Event.jpg\" alt=\"Add Event\" title=\"Add Event\" height=\"20px\" width=\"20px\" style=\"padding-left:20px;\"/></a><br/>".$event_title."</td>\n";
         unset($event_title);
     
            $start += ADAY;
        }
    }
    echo "</tr></table>";
    mysql_close($con);
    ?>

</body>
</html>

 

Please help me out its urgent..

 

 

Thank you in advance...

Link to comment
Share on other sites

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.