Event calendar

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Event calendar

Post by vchris »

Hey!

I am trying to create an event calendar when hockey players can see when they play their games. I kinda got some problems with the code.

Code: Select all

//Connect to the DB
require_once('mysql_connect.php');

//Make the query
$query = "SELECT scheduleid, DAYOFMONTH(sdate) AS sd, steam, sarena, supdate FROM schedule";

//Run the query
$result = @mysql_query($query);

//If it ran OK, display the records
if($result) {
	
	
		//Fetch and print all the records.
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){[/color]
		
		
		// How many days are in the current month
		$Days_In_Month = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
		
		// Gets the Current day, day with 1st 2nd etc, day name, year (2004), month,
		// month name
		$Current_Day = date("d");
		$Current_Day_S = date("dS");
		$Current_Day_Name = date("l");
		$Current_Year = date("Y");
		$Current_Month = date("m");
		$Current_Month_Name = date("F");
		
		// Get the offset of the first day of the month
		$First_Day_Of_Month = date("w", mktime(0, 0, 0, $Current_Month, 1, $Current_Year));
		
		// Set the day names
		$Days_Array = array();
		$Days_Array[] = "Sun";
		$Days_Array[] = "Mon";
		$Days_Array[] = "Tues";
		$Days_Array[] = "Wed";
		$Days_Array[] = "Thurs";
		$Days_Array[] = "Fri";
		$Days_Array[] = "Sat";
		
		// For each of the Day Names, print em out
		$Day_Names = "";
		foreach ($Days_Array as $x => $y) {
			$Day_Names .= '<th class="tableheader">' . $y . '</th>';
		}
		
		// Spacers for the offset of the first day of the month
		$Cal_Weeks_Days = "";
		$i = $First_Day_Of_Month + 1;
		if ($First_Day_Of_Month != "0") {
			$Cal_Weeks_Days .= '<td colspan="' . $First_Day_Of_Month . '"> </td>';
		}

		
		
		// Cal Days - The first day is 1, default with PHP is 0, so lets set it to 1
		$Day_i = "1";
		$ii = $i;
		for ($i; $i <= ($Days_In_Month + $First_Day_Of_Month) ;$i++) {
			// $i is our color variable - Alternate row colors:
			if ($i % 2) {
				$color = '#FFAAFF';
			}
			else
			{
				$color = '#FFFFAA';
			}
		
			// If the current day is sunday, make sure a new row gets set
			if ($ii ==  {
				$Cal_Weeks_Days .= "</tr><tr>";
				$ii = 1;
			}
						
			// If the day is the current day, highlight it with a special color
			if ($row['sd'] == $Day_i) {
				$eventinfo = $row['steam'];
			}
			else
			{
				// Alternate row colors
				$eventinfo = null;
			}
		
			// Show the days.
			$Cal_Weeks_Days .= '<td><span class="bold">' . $Day_i . '</span><br>'  . $eventinfo . '</td>';
		
			// Increment the day number and the week day number (ii)
			$Day_i++;
			$ii++;
		}
		
		// Add end month spacers
		if ((8 - $ii) >= "1") {
			$Cal_Weeks_Days .= '<td colspan="' . (8 - $ii) . '"> </td>';
		}

}
	//Free up the ressources
	mysql_free_result($result);
}
//If it did not run OK.
else {
	echo '<h3>News could not be retrieved</h3>';
}

//Close Connection
mysql_close();

?>

<h1><?php echo "$Current_Month_Name $Current_Year"; ?></h1>
<table cellpadding="0" cellspacing="1" class="scheduletable" align="center">
<tr>
	<?php echo "$Day_Names"; ?>
</tr>
<tr>
	<?php echo "$Cal_Weeks_Days"; ?>
</tr>
<tr>
	<th colspan="7">Abbreviations: AG=Atom Girls, PG=PeeWee Girls, MG=Midget Girls, BG=Bantam Girls</th>
</tr>
</table>
What is in red is what I need to display the events. I know it's at the wrong spot. The way I got it right now, every time there is another record is redoes the calendar for this month. But I want the calendar to contain all events in september.

By the way in the column "sdate" it's a date/time column. I need to display the time as well.

I need some help!!!

Burrito: Please use

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]posting code in the forum[/url].[/size]
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

no one can help me with this?
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

How could I display the events (steam) within the september month???
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

Or does anyone know anywhere where I could find code for a calendar where I could easily include my events from the database?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

okay..this code is long and drawn out and taken from a free script but modified to pull events from a database. i also recommen djust pulling the time from a database also..here it is...

Code: Select all

<?php
// Birthday Calendar. The function is not in the function page because this calendar functions
// differently from the ohter calendar and I have not quite got around to standardizing it. In the
// future this will be probably be corrected.
if ($_SESSION['online']=='yes')
{
?>
<?php
   # PHP Calendar (version 2.3), written by Keith Devens
   # http://keithdevens.com/software/php_calendar
   #  see example at http://keithdevens.com/weblog
   # License: http://keithdevens.com/software/license
   global $requestmonth; //probably not even nessecary:-D
   $requestmonth=$_REQUEST['month'];
   $year=$_REQUEST['year']+1; // due to the differences of the way we did our times
  // adding a year was nessecary to make hte script function properly, I know it's not the
 // greatest ut it does function.
   $yeare=$_REQUEST['year']; //needed the year for other calc's.

   function generate_calendar($year, $month, $requestmonth, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array())
   {
	   $first_of_month = gmmktime(0,0,0,$month,1,$year);
	   #remember that mktime will automatically correct if invalid dates are entered
	   # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
	   # this provides a built in "rounding" feature to generate_calendar()

	   $day_names = array(); #generate all the day names according to the current locale
	   for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
	      $day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name

	   list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
	   $weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
	   $title   = htmlentities(ucfirst($month_name)).'&nbsp;'.$year;  #note that some locales don't capitalize month and day names

	   #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
	   @list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
	   if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span>&nbsp;';
	   if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
	   $calendar = '<table width="100%" border="1">'."\n".
	   '<div align="center">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."\n</div><tr>";

	   if($day_name_length)
	   { #if the day names should be shown ($day_name_length > 0)
		#if day_name_length is >3, the full name of the day will be printed
		foreach($day_names as $d)
			$calendar .= '<td width="14%" align="center" bgcolor="#CCCCCC" border="1" abbr="'.htmlentities($d).'"><b>'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</b></td>';
		$calendar .= "</tr>\n<tr>";
	   }

	   if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; #initial 'empty' days
	   for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++)
	   {
	      if($weekday == 7)
		   {
			   $weekday   = 0; #start a new week
			   $calendar .= "</tr>\n<tr>";
		   }
		   if ($day<10)
		   {
			   $day="0$day";
		   }
		   $specialmonth=$requestmonth-1;
		   if ($specialmonth<10)
		   {
			   $specialmonth="0$specialmonth";
		   }
		   $bdayquery="SELECT * FROM `member` WHERE `Bday` LIKE '".$specialmonth.$day."%'";
              // above you query the database! 
		   $bdayresult=mysql_query($bdayquery);
		   $number=mysql_num_rows($bdayresult);
// above statement is rather important:-D
		
		   if(isset($days[$day]) and is_array($days[$day]))
		   {
			 @list($link, $classes, $content) = $days[$day];
			 if(is_null($content))  $content  = $day;
			  $calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
				($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
		   }
		   else
		   {
		      $calendar .= "<td height='85px' valign='top'><div align='left'>$day</div><br><div align='center'>"; //normal days
		      if ($number>=1)
		      {
			    while ($bdayresultquery=mysql_fetch_object($bdayresult))
			    {
			     $calendar .= "$bdayresultquery->Fname $bdayresultquery->Lname<br>";
			    }
		      }
		    }
	    }
	   if($weekday != 7) $calendar .= '<td height="85px" colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days
// I am VERY aware that i did not close the td, mainly because everytime i try too it messes up hte table
// appearntly if I let the browser figure out hte bad code it displays right.. sometimes i just go with what works
// sorry.
	   return $calendar."</tr>\n</table>\n";
	}
?>
<?php
    $time = mktime(0,0,0,0,0,"$year");
	$timem= mktime(0,0,0,"$requestmonth",0,0);
?>
// here starts some output to the actual page.
<div align="center">You can change the month and year by selecting the appropriate links below.</div>
<table width="100%" border="1">
  <tr>
    <?php
	for ($i=5; $i<17;$i++)
	{
		while ($i<10)
		{
			echo ("<td align='center'><a href='index.php?page=birthday&month=$requestmonth&year=200$i'>200$i</a></td>\n");
			$i++;
		}
		while ($i>9 && $i<17)
		{
			echo ("<td align='center'><a href='index.php?page=birthday&month=");
			if ($requestmonth<10)
			{
				echo ("$requestmonth");
			}
			else
			{
				echo ("$requestmonth");
			}
			echo ("&year=20$i'>20$i</a></td>\n");
			$i++;
		}
	}
	?>
  </tr>
  <tr>
    <?php
			for ($i=2;$i<=13;$i++)
			{
				$monthselected=date("F",mktime(0,0,0,$i,0,$year));
				echo ("<td align='center'> <a href='index.php?page=birthday&month=");
				if ($i<10)
				{
					echo ("0$i");
				}
				else
				{
					echo ("$i");
				}
				echo ("&year=$yeare'>$monthselected</a></td>\n");
			}
		?>
  </tr>
</table>
<br>
<?php
    echo generate_calendar(date('Y', $time), date('n', $timem), $requestmonth, NULL, 2);
}
?>
whew. i hope htat helps...
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

ah man this is so confusing. I'm trying to modify the code you gave me to make it work but it's so complicated. That script is a bit different then what I got. I have a script that display the month. You found anything like that?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

bah. to sum it up. change the database query to what you need to display. and in your URL have hte code read something like http://www.yourpage.comindex.php?month=11&year=2005 . that's it. that'll change it as much as you need it too probably:-D
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

Ok I'm starting to get this! :D

Just need to mod a few things. Thanks a lot!
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

lol.well i never promised it'd be a custom fit but it's what I did for my website. I still don't understand SOME of the code:-D but eh, it does work. that i promise. glad to help my friend.
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

I have just one problem though. Here is my query:

Code: Select all

SELECT scheduleid, DAYOFMONTH(sdate) AS sd, steam, sarena, supdate FROM schedule
The reason why I have "DAYOFMONTH(sdate) AS sd" is to get the day of the event but in that column I also have the time which I also need in this script.

How would I display the time of the event with this query or what would I have to change?
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

nvm I got it!
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

good job man. feel free to PM me if you get stuck
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

Same for you. I appreciate the help! This was the most complicated part of the site. It's perfect now :D
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

vchris wrote:Or does anyone know anywhere where I could find code for a calendar where I could easily include my events from the database?
Give this a try: http://calendar.codewalkers.com/
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i'd go for php icalendar.. as it talks ical :)
Post Reply