mysql - PHP: Multiple events in an ics file generated via php? -


i'm generating ics file using php , works fine long ics file contains 1 event in it.

however, i'm trying put multiple events inside same ics file.

i'm using information stored inside mysql generate ics file.

the issue have when run code, no error @ no ics file being generate either!

this entire code:

<?php class ics {     var $data;     var $name;     function ics($start,$end,$name,$description,$location) {         $this->name = $name;         $this->data = "begin:vcalendar\nversion:2.0\nmethod:publish\nbegin:vevent\ndtstart:".date("ymd\this\z",strtotime($start))."\ndtend:".date("ymd\this\z",strtotime($end))."\nlocation:".$location."\ntransp: opaque\nsequence:0\nuid:\ndtstamp:".date("ymd\this\z")."\nsummary:".$name."\ndescription:".$description."\npriority:1\nclass:public\nbegin:valarm\ntrigger:-pt10080m\naction:display\ndescription:reminder\nend:valarm\nend:vevent\nend:vcalendar\n";     }     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 * mytable" ;      // ------- make sure person exists in database ---------     $query = mysqli_query($db_conx, $sql);     $existcount = mysqli_num_rows($query); // count row nums     if ($existcount == 1) { // evaluate 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 remove line of code , place outside while loop, everrything works again generate ics file has 1 event in it!

       $event = new ics("".$start_date."","".$end_date."","".$title."","".$details."","".$location."");        $event->show(); 

could please advise on issue?

thanks in advance.

edit:

i ics file created wont imported calendar , failed error when try add event in calendar:

<?php class ics {     var $data;     var $name;     function ics($start,$end,$name,$description,$location) {         $this->name = $name;         $this->data = "begin:vcalendar\nversion:2.0\nmethod:publish\nbegin:vevent\ndtstart:".date("ymd\this\z",strtotime($start))."\ndtend:".date("ymd\this\z",strtotime($end))."\nlocation:".$location."\ntransp: opaque\nsequence:0\nuid:\ndtstamp:".date("ymd\this\z")."\nsummary:".$name."\ndescription:".$description."\npriority:1\nclass:public\nbegin:valarm\ntrigger:-pt10080m\naction:display\ndescription:reminder\nend:valarm\nend:vevent\nend:vcalendar\n";     }     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 * mytable";      // ------- make sure person exists in database ---------     $query = mysqli_query($db_conx, $sql);     $existcount = mysqli_num_rows($query); // count row nums     if ($existcount == 1) { // evaluate 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 answer given bellow, i've changed code doesn't create ics file @ , 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\ndtstart:".date("ymd\this\z",strtotime($start))."\ndtend:".date("ymd\this\z",strtotime($end))."\nlocation:".$location."\ntransp: opaque\nsequence:0\nuid:\ndtstamp:".date("ymd\this\z")."\nsummary:".$name."\ndescription:".$description."\npriority:1\nclass:public\nbegin:valarm\ntrigger:-pt10080m\naction:display\ndescription:reminder\nend:valarm\nend:vevent\n";     }      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\nversion:2.0\nmethod:publish\n".$this->event."end:vcalendar\n";             header('connection: close');         }     } } ?> <?php $event = new ics;      $sql = "select * mytable";      // ------- make sure person exists in database ---------     $query = mysqli_query($db_conx, $sql);     $existcount = mysqli_num_rows($query); // count row nums     if ($existcount == 1) { // evaluate 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, looks i'm getting somewhere... i've changed code following , when run php page, test.ics file generated when open it, empty. there nothing in whatsoever!

<?php class ics {     var $data;     var $name ='test';     function addevent($start, $end, $name, $description, $location) {         $this->name = $name;         $this->event .= "begin:vevent\ndtstart:".date("ymd\this\z",strtotime($start))."\ndtend:".date("ymd\this\z",strtotime($end))."\nlocation:".$location."\ntransp: opaque\nsequence:0\nuid:\ndtstamp:".date("ymd\this\z")."\nsummary:".$name."\ndescription:".$description."\npriority:1\nclass:public\nbegin:valarm\ntrigger:-pt10080m\naction:display\ndescription:reminder\nend:valarm\nend:vevent\n";     }      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\nversion:2.0\nmethod:publish\n".$this->data."end:vcalendar\n";          header('connection: close');     } } ?> <?php $event = new ics;      $sql = "select * mytable" ;      // ------- make sure person exists in database ---------     $query = mysqli_query($db_conx, $sql);     $existcount = mysqli_num_rows($query); // count row nums     if ($existcount == 1) { // evaluate 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();   ?> 


Comments