0

Hi,

I have implemented an event calendar script. Its working fine. But I want to display the calendars of 3 consecutive months on selecting the month and year from drop down menus on submit with the selected month and year in between the other two as in Easy PHP calendar. Then on clicking the next and previous buttons the respective month calendars should slide. Later i should be able to add events on clicking the date.

Will be extremely happy if anyone could help me.

The one which I have executed is http://www.calendar.iconwebservices.com/calendar.php


Thank You in advance.

2
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by diafol
0

So if you choose Nov 2011, you should see Oct 2011 | Nov 2011 | Dec 2011. Is that it?

Prev | Next buttons 'slide' calendar by one month increment either way?

Not knowing how you've constructed your calendar, it's difficult to know what you need help with.

The 'slide effect' if that's what you're looking for can be done with javascript (jQuery is good for this), however, there are so many good datepickers out there, unless you're doing a 'self-improvement' project, you may be better off using one of these:

Attachments Capture.PNG 14.8 KB
0

So if you choose Nov 2011, you should see Oct 2011 | Nov 2011 | Dec 2011. Is that it?

Prev | Next buttons 'slide' calendar by one month increment either way?

Not knowing how you've constructed your calendar, it's difficult to know what you need help with.

The 'slide effect' if that's what you're looking for can be done with javascript (jQuery is good for this), however, there are so many good datepickers out there, unless you're doing a 'self-improvement' project, you may be better off using one of these:

[ATTACH]23085[/ATTACH]

Yes. That's exactly what I require as seen in http://www.easyphpcalendar.com/v6demo/events/index.php?PHPSESSID=2e191d9b4aeee7350d71b8ab49a1e2ae.

Please help me out.

My code is as follows

calendar.php

<?php
	$host="localhost"; // Host name
	$user=""; // Mysql username
	$pass=""; // Mysql password
	$db_name=""; // Database name
	$tbl_name=""; // Table name
	
	// At line 2 of our calendar.php script, add the MySQL connection information:
	$mysql = mysql_connect("$host", "$user", "$pass")or die("cannot connect");
	mysql_select_db("$db_name", $mysql) or die(mysql_error());
	 
	// 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($_POST['month'])) || (!isset($_POST['year']))) {
	    $nowArray = getdate();
	    $month = $nowArray['mon'];
    $year = $nowArray['year'];
	} else {
	    $month = $_POST['month'];
    $year = $_POST['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 rel="stylesheet" type="text/css" href="calendar.css"/>-->
</head>
<script type="text/javascript">
	function eventWindow(url) {
	    event_popupWin = window.open(url, 'event', 'resizable=yes,scrollbars=yes,toolbar=no,width=400,height=400');
	    event_popupWin.opener = self;
	}
</script>

<body>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	<select name="month">
	<?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!">
	</form>
	<br />
	<?php
	$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	echo "<table border=\"1\"><tr>\n";
	foreach ($days as $day) {
	    echo "<td style=\"background-color: #CCCCCC; width: 50px\">
	          <strong>$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) {
        echo "<td> </td>\n";
	    } else {
	        $chkEvent_sql = "SELECT event_title 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, $mysql) or die(mysql_error($mysql));
 
        if (mysql_num_rows($chkEvent_res) > 0) {
            $event_title = "<br/>";
            while ($ev = mysql_fetch_array($chkEvent_res)) {
	                $event_title .= stripslashes($ev["event_title"])."<br/>";
            }
	            mysql_free_result($chkEvent_res);
	        } else {
            $event_title = "";
	        }
 
        echo "<td ><a href=\"event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year\">".$dayArray["mday"]."</a><br/>".$event_title."</td>\n";
	 	unset($event_title);
	 
	        $start += ADAY;
	    }
	}
	echo "</tr></table>";
	mysql_close($mysql);
	?>
</body>
</html>

event.php

<?php
	$host="localhost"; // Host name
	$user=""; // Mysql username
	$pass=""; // Mysql password
	$db_name=""; // Database name
	$tbl_name=""; // Table name
	
	$mysql = mysql_connect("$host", "$user", "$pass")or die("cannot connect");
	mysql_select_db("$db_name", $mysql) or die(mysql_error());
	 
	// Add our new events
	if ($_POST){
	    $m = $_POST['m'];
	    $d = $_POST['d'];
	    $y = $_POST['y'];
	 
	    // Formatting for SQL datetime (if this is edited, it will NOT work.)
	    $event_date = $y."-".$m."-".$d." ".$_POST["event_time_hh"].":".$_POST["event_time_mm"].":00";
	 
	    $insEvent_sql = "INSERT INTO calendar_events (event_title,
	            event_shortdesc, event_start) VALUES('
	            ".$_POST["event_title"]."',
	            '".$_POST["event_shortdesc"]."', '$event_date')";
	    $insEvent_res = mysql_query($insEvent_sql, $mysql)
	            or die(mysql_error($mysql));
	} else {
	    $m = $_GET['m'];
	    $d = $_GET['d'];
	    $y = $_GET['y'];
	}
	// Show the events for this day:
	$getEvent_sql = "SELECT event_title, event_shortdesc,
	        date_format(event_start, '%l:%i %p') as fmt_date FROM
	        calendar_events WHERE month(event_start) = '".$m."'
	        AND dayofmonth(event_start) = '".$d."' AND
	        year(event_start)= '".$y."' ORDER BY event_start";
	$getEvent_res = mysql_query($getEvent_sql, $mysql)
	        or die(mysql_error($mysql));
	 
	if (mysql_num_rows($getEvent_res) > 0){
    $event_txt = "<ul>";
	    while($ev = @mysql_fetch_array($getEvent_res)){
	        $event_title = stripslashes($ev["event_title"]);
	        $event_shortdesc = stripslashes($ev["event_shortdesc"]);
	        $fmt_date = $ev["fmt_date"];
	        $event_txt .= "<li><strong>".$fmt_date."</strong>:
	                  ".$event_title."<br/>".$event_shortdesc."</li>";
	    }
	    $event_txt .="</ul>";
	    mysql_free_result($getEvent_res);
	} else {
	    $event_txt = "";
	}
	 
	mysql_close($mysql);
	 
	if ($event_txt != ""){
	    echo "<p><strong>Today's Events:</strong></p>
	    $event_txt
	    <hr/>";
	}
	 
	// Show form for adding the event:
	 
	echo "
	<form method=\"post\" name=\"form1\" action=\"".$_SERVER['PHP_SELF']."\">
	<p><strong>Add Event:</strong><br/>
	Complete the form below then press the submit button when you are done.</p>
	<p><strong>Event Title:</strong><br/>
	<input type=\"text\" name=\"event_title\"/></p>
	<p><strong>Event Date:</strong><br/>
	<input type=\"text\" name=\"event_date\" value=\"\"/></p>
	<p><strong>Event Description:</strong<br/>
	<input type=\"text\" name=\"event_shortdesc\"/></p>
	<p><strong>Event Time (hh:mm):</strong><br/>
	<select name=\"event_time_hh\">";
	for ($x=00; $x<=23; $x++){
	    echo "<option value=\"$x\">$x</option>";
	}
	echo "</select> :
	<select name=\"event_time_mm\">
	<option value=\"00\">00</option>
	<option value=\"15\">15</option>
	<option value=\"30\">30</option>
	<option value=\"45\">45</option>
	</select>
<input type=\"hidden\" name=\"m\" value=\"".$m."\">
	<input type=\"hidden\" name=\"d\" value=\"".$d."\">
	<input type=\"hidden\" name=\"y\" value=\"".$y."\">
<br/><br/>
	<input type=\"submit\" name=\"submit\" value=\"Add Event!\" onclick=\"return validate();\">
	</form>";
	?>
	
	<a href="calendar.php"><input type="button" value="View Calendar" /></a>
0

IMO, you either need to make a class out of this or at least change it to a function, so that you can do something like this:

OOP:

$cal = new Calendar($month,$year);
$thisMonth=$cal->getThisMonth();
$nextMonth=$cal->getNextMonth();
$lastMonth=$cal->getLastMonth();

Vanilla Function:

$thisMonth = getCalendar('now');
$nextMonth = getCalendar('before');
$previousMonth = getCalendar('after');

For the function version:

function getCalendar($when){
   $month = date('n'); //default month if no posted form
   $year = date('Y'); //default year if no posted form
   if(isset($_POST['month']) && intval($_POST['month']) > 0 && intval($_POST['month']) < 13)$month = intval($_POST['month']);
   if(isset($_POST['year']) && intval($_POST['year']) > 2000 && intval($_POST['year']) < 2051)$month = intval($_POST['year']);
   if(isset($_POST['submitPrev']))$month = $month - 1;//if form submitted with previous button
   if(isset($_POST['submitNext']))$month = $month + 1;//if form submitted with next button 
   if($when=='before')$month = $month - 1;
   if($when=='after')$month = $month + 1;
   $startdate = mktime(0,0,0,$month,1,$year); //calculate right date even if month overflows to -1 or 14 - I ASSUME
   
   //do your code to display a calendar and concatenate it into a variable(e.g. $output)

   return $output;
}

The datepicker form, something like that?:

<form method="post">
  <button type="submit" name="submitPrev">Prev</button>
  <!-- month, year and normal submit controls here -->
  <button type="submit" name="submitNext">Next</button>
</form>

For displaying the calendar:

echo getCalendar('before');
echo getCalendar('now');
echo getCalendar('next');

Anyway, this is off the top of my head - none of it has been tested. If your code works for producing a single calendar, it *should* work for 3. The bit I'm not so sure about is the mktime() bit.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.