Create Event VCS File

I have the following code that writes out information on an event that someone selects from our website. How do I create a "Save to Outlook Calendar" link from this information?
<?PHP 
$listing_id = $_GET['listing_id'];

$sql = "SELECT * FROM Listings WHERE ID='$listing_id'";

$sql_query = mysql_query($sql);



if (mysql_num_rows($sql_query) == 0)

{

	echo "Listing ID does not exist<br>\n";

	exit;

}


$listing_row = mysql_fetch_assoc($sql_query);

$ListingID = $listing_row['ID'];

$ListingName = $listing_row['Name'];

$ListingStartDate = $listing_row['EventStartDate'];

$ListingStartHour = $listing_row['EventStartHour'];

$ListingStartMinute = $listing_row['EventStartMinute'];

$ListingStartAMorPM = $listing_row['EventStartAMorPM'];

$ListingEndHour = $listing_row['EventEndHour'];

$ListingEndMinute = $listing_row['EventEndMinute'];

$ListingEndAMorPM = $listing_row['EventEndAMorPM'];

$ListingVenue = $listing_row['Venue'];

$ListingVenueAddress = $listing_row['VenueAddress'];

?> 

  <?php 
	echo "$ListingName<br />";
	echo "$ListingStartDate<br />";
echo "$ListingStartHour:$ListingStartMinute $ListingStartAMorPM\n":
?>

Open in new window

katleesAsked:
Who is Participating?
 
Ray PaseurCommented:
Here is a very old example from one of my sites.  Sorry about the spacing.  It works.  Hopefully it can show you a design pattern you can adapt for your own calendar needs.  HTH, ~Ray
<?php // calendar_vcal.php
require_once('common.php');
error_reporting(E_ALL ^ E_NOTICE);
$_key		= $_GET["k"];
if (!integer_string($_key)) die("Bad Key");

$filename = "LBBEvent" . $_key . ".vcs";
header("Content-Type: text/x-vCalendar");
header("Content-Disposition: inline; filename=$filename");

$sql 	= "SELECT * FROM calendar WHERE _key = $_key LIMIT 1 ";
if (!$res = mysql_query($sql)) { query_error($sql); }
$num_rows = mysql_num_rows($res);

if ($num_rows == 0) {
	echo "Sorry, the <b>Event Key $_key</b> is not found in the calendar.<br /><br />\n";
	echo "Please report this error to the curator.<br /><br />\n";
	die("Sorry, Unable to add to your calendar.");
}
if ($num_rows != 1) {
	echo "SQL $sql<br>";
	echo "RESULT $result<br><br>";
	echo "The <b>Event Key $_key</b> appears to have $num_rows data base entries in the calendar (yikes!)<br />\n";
 	echo "Please report this error to the curator.<br /><br />\n";
 	die("Sorry, Unable to add to your calendar.");
}

$calendar_array = mysql_fetch_assoc($res); // NOTE WE DO NOT TRANSFER THE CATEGORY TO THE V-CAL
foreach ($calendar_array as $k => $v) { $$k = $v; }

$loc					= trim($location);
if ($loc  != '') { $loc  = '=0D=0A=Location: ' . $loc; }
$poc1					= trim($poc1_name .' '. $poc1_phone .' '. $poc1_email);
if ($poc1 != '') { $poc1 = '=0D=0A=Contact: ' . $poc1; }
$poc2					= trim($poc2_name .' '. $poc2_phone .' '. $poc2_email);
if ($poc2 != '') { $poc2 = '=0D=0A=Contact: ' . $poc2; }
$pocw					= trim($poc_url);
if ($pocw != '') { $pocw = '=0D=0A=WWW: ' . $pocw; }
$poc = $poc1 . $poc2 . $pocw;

// CALENDAR TOLERATES END TIMESTAMPS WRONG OR OMITTED - VCAL DOES NOT
$vcal_dtstart = date('Y-m-d\TH:i:sO', strtotime($start));
$vcal_dtend   = date('Y-m-d\TH:i:sO', strtotime($end));
if ($vcal_dtend < $vcal_dtstart) { $vcal_dtend = $vcal_dtstart; }

?>BEGIN:VCALENDAR
VERSION:1.0
PRODID:LBB Online Calendar
BEGIN:VEVENT
SUMMARY:<?php echo "$title" . "\n"; ?>
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:<?php echo "$details" . "$loc" . "$poc" . "\n"; ?>
DTSTART:<?php echo $vcal_dtstart . "\n"; ?>
DTEND:<?php echo $vcal_dtend . "\n"; ?>
END:VEVENT
END:VCALENDAR

Open in new window

0
 
jimbobmcgeeCommented:
Not a PHP expert but you would need to redirect to a page that generates the text like the following and has a Content-Type MIME header of either text/x-vcalendar, text/calendar or application/hbs-vcs:

BEGIN:VCALENDAR
BEGIN:VEVENT
STATUS:TENTATIVE
DTSTART:20101230T033000Z
DTEND:20101230T043000Z
SUBJECT:Your Proposal Review
DESCRIPTION:Testing
CLASS:PRIVATE
END:VEVENT
END:VCALENDAR
0
 
katleesAuthor Commented:
Thanks Jim.. I saw that much just don't know how to convert my current code to show all of this...
0
 
jimbobmcgeeCommented:
At a pure guess, I'd say it might look like the following (although I expect an experienced PHP developer would pull it to pieces):
<?php
    header('Content-Type: text/x-vcalendar')
    header('Content-Disposition: attachment; filename="vcal.vcs"');

    $ListingStart = $ListingStartDate + ' ' + $ListingStartHour + ':' + $ListingStartMinute + ' ' + $ListingStartAMorPM;
    $ListingEnd = $ListingEndDate + ' ' + $ListingEndHour + ':' + $ListingEndMinute + ' ' + $ListingEndAMorPM;

    echo 'BEGIN:VCALENDAR\r\n';
    echo 'BEGIN:VEVENT\r\n';

    echo 'UID:';
    echo 'www.mysite.com-vcal-';
    echo $ListingID;
    echo '\r\n';

    echo 'DTSTART:'
    echo date("Ymd", strtotime($ListingStart))
    echo 'T'
    echo date('his', strtotime($ListingStart))
    echo 'Z\r\n'

    echo 'DTEND:'
    echo date('Ymd', strtotime($ListingEnd))
    echo 'T'
    echo date('his', strtotime($ListingEnd))
    echo 'Z\r\n'

    echo 'SUMMARY;ENCODING=QUOTED-PRINTABLE:'
    echo quoted_printable_encode($ListingName)
    echo '\r\n'

    echo 'LOCATION;ENCODING=QUOTED-PRINTABLE:'
    echo quoted_printable_encode($ListingVenue)
    echo '\r\n'

    echo 'DESCRIPTION;ENCODING=QUOTED-PRINTABLE:'
    echo quoted_printable_encode($ListingVenueAddress)
    echo '\r\n'

    echo 'END:VEVENT\r\n';
    echo 'END:VCALENDAR\r\n\r\n\r\n';
?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.