I'm generating an ics
file using php and everything works fine as long as the ics file only contains one event in it.
However, I'm trying to put multiple events inside the same ics file.
I'm using the information stored inside the MYSQL to generate the ics file.
The issue that I have is that when I run my code, I get no error at all but no ics file being generate either!
This is my entire code:
<?php
class ICS {
var $data;
var $name;
function ICS($start,$end,$name,$description,$location) {
$this->name = $name;
$this->data = "BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
DTSTART:".date("Ymd\THis\Z",strtotime($start))."
DTEND:".date("Ymd\THis\Z",strtotime($end))."
LOCATION:".$location."
TRANSP: OPAQUE
SEQUENCE:0
UID:
DTSTAMP:".date("Ymd\THis\Z")."
SUMMARY:".$name."
DESCRIPTION:".$description."
PRIORITY:1
CLASS:PUBLIC
BEGIN:VALARM
TRIGGER:-PT10080M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="'.$this->name.'.ics"');
Header('Content-Length: '.strlen($this->data));
Header('Connection: close');
echo $this->data;
}
}
?>
<?php
$sql = "SELECT * FROM mytable" ;
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event .= new ICS("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
$event->show();
}
}
?>
if i remove this line of code and place it outside my while loop, everrything works again but it will generate an ics file that only has 1 event in it!
$event = new ICS("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
$event->show();
Could someone please advise on this issue?
Thanks in advance.
EDIT:
I now get the ics file created but it wont get imported into the calendar and I get a failed error when I try to add the event in the calendar:
<?php
class ICS {
var $data;
var $name;
function ICS($start,$end,$name,$description,$location) {
$this->name = $name;
$this->data = "BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
DTSTART:".date("Ymd\THis\Z",strtotime($start))."
DTEND:".date("Ymd\THis\Z",strtotime($end))."
LOCATION:".$location."
TRANSP: OPAQUE
SEQUENCE:0
UID:
DTSTAMP:".date("Ymd\THis\Z")."
SUMMARY:".$name."
DESCRIPTION:".$description."
PRIORITY:1
CLASS:PUBLIC
BEGIN:VALARM
TRIGGER:-PT10080M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
echo $this->data;
}
}
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="test.ics"');
Header('Content-Length: '.strlen($this->data));
?>
<?php
$sql = "SELECT * FROM mytable";
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event .= new ICS("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
}
}
$event->show();
Header('Connection: close');
?>
SECOND EDIT:
Based on the answer given bellow, I've changed my code to look like this but this doesn't create any ics file at all and no errors either:
<?php
date_default_timezone_set("Europe/London");
class ICS {
private $event;
private $name;
function addEvent($start, $end, $name, $description, $location) {
$this->name = $name;
$this->event .= "BEGIN:VEVENT
DTSTART:".date("Ymd\THis\Z",strtotime($start))."
DTEND:".date("Ymd\THis\Z",strtotime($end))."
LOCATION:".$location."
TRANSP: OPAQUE
SEQUENCE:0
UID:
DTSTAMP:".date("Ymd\THis\Z")."
SUMMARY:".$name."
DESCRIPTION:".$description."
PRIORITY:1
CLASS:PUBLIC
BEGIN:VALARM
TRIGGER:-PT10080M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
";
}
function output() {
if ($this->event) {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="'.$this->name.'.ics"');
Header('Content-Length: '.strlen($this->event));
echo "BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
".$this->event."END:VCALENDAR
";
Header('Connection: close');
}
}
}
?>
<?php
$event = new ICS;
$sql = "SELECT * FROM mytable";
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event->addEvent($start_date,$end_date,$title,$details,$location);
}
}
$event->output();
?>
THIRD EDIT
Okay, it looks like I'm getting somewhere... I've changed my code to the following and when I run my php page, I get the test.ics file generated but when i open it, it empty. there is nothing in it whatsoever!
<?php
class ICS {
var $data;
var $name ='test';
function addEvent($start, $end, $name, $description, $location) {
$this->name = $name;
$this->event .= "BEGIN:VEVENT
DTSTART:".date("Ymd\THis\Z",strtotime($start))."
DTEND:".date("Ymd\THis\Z",strtotime($end))."
LOCATION:".$location."
TRANSP: OPAQUE
SEQUENCE:0
UID:
DTSTAMP:".date("Ymd\THis\Z")."
SUMMARY:".$name."
DESCRIPTION:".$description."
PRIORITY:1
CLASS:PUBLIC
BEGIN:VALARM
TRIGGER:-PT10080M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename=test.ics');
Header('Content-Length: '.strlen($this->data));
//echo $this->data;
echo "BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
".$this->data."END:VCALENDAR
";
Header('Connection: close');
}
}
?>
<?php
$event = new ICS;
$sql = "SELECT * FROM mytable" ;
// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$query = mysqli_query($db_conx, $sql);
$existCount = mysqli_num_rows($query); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$details = $row["details"];
$location = $row["location"];
$start_date = $row["start_date"];
$end_date = $row["end_date"];
$title = $row["title"];
$start0 = str_replace("/","-",$start_date);
$end0 = str_replace("/","-",$end_date);
/* $start_date = date("Y-m-d H:i");*/
$end_date = strtotime($end_date . ' -1 hour');
$end_date = (date('Y-m-d H:i', $end_date));
$start_date = strtotime($start_date . ' -1 hour');
$start_date = (date('Y-m-d H:i', $start_date));
$event->addEvent("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");
}
}
$event->show();
?>