Create downloadable iCal files in PHP/MySQL/Bootstrap?
-
I have a PHP webpage that lists events happening at a conference by reading from a MySQL database. Is there an easy way to have PHP create downloadable iCal files so people can download individual events to their iphones/Androids etc., without my having to create individual event files, and without setting up a bunch of Google Calendar items to link to? (more detailed explanation inside.) Website is for a four day conference, several individual event listings per day. It's only a yearly thing, so for that and other reasons we've decided that a full blown CMS system with a calendaring function is total overkill. I'm redesigning the site using Twitter Bootstrap, mainly to modernize the layout and CSS type stuff, but I'll be keeping it in PHP so I can maintain a few server includes I use that help me change over certain things year to year more easily. For years I've just had the events page read from an "events" table in MySQL, that has basic columns - event ID#, date, start time, end time, name, location, description. The PHP formats that into tables to display on the webpage. What I would love, would be for each event listing to have an iCal download link. But I'd like to not have to somehow manually create the iCal files. I was hoping there might be some way PHP could sort of write the iCal files on the fly or something like that? I suppose I could enter them into a Google Calendar, and paste the individual links into the database for inclusion on the website. But again that would create a lot of manual work to enter all these events. The folks who run the conference just keep the event listings in Word, frankly. I end up reformatting to Excel/CSV for importing to the database. So if there were some PHP magic that could be done, it would be able so save me work each year. I'm not very programming/scripting savvy, but some searching led me to believe this might be possible. I thought I might do better asking here where I could maybe get someone to talk me through it a bit while being patient with my pretty basic level PHP skills.
-
Answer:
The quick and dirty way is to probably tack whatever variable you need onto the end of the link e.g."make_my_ical_file.php?myvar=myvarvalue" in which case it's a GET variable which is accessed in php by $_GET['myvar'] and you can do whatever queries/logic etc. you need to with it inside make_my_ical_file.php You can do multiple variables by "make_my_ical_file.php?myfirstvar=myfirstvarvalue&mysecondvar=mysecondvarvalue" etc. Another way is to scrap using <a> and make an http://php.net/manual/en/tutorial.forms.php. Then if you give the form tag the attributes action="make_my_ical_file.php" and method="post" or method="get" the various form elements will map to $_POST['input_element_name'] and $_GET['input_element_name'] respectively if you hit submit. Be aware that there are security implications if you don't properly validate the contents of those variables before using them. In general, if you are doing database-y stuff, you should look into using http://php.net/manual/en/pdo.prepared-statements.php.
dnash at Ask.Metafilter.Com Visit the source
Other answers
Writing iCal files on the fly is very doable. The file format is quite straight-forward and includes only plain text. The trickiest part is handling time zones (at least it was for me). http://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calen might get you started. I write Ruby more than PHP, so I don't have a lot of specific advice, but may be able to help anyway if you get stuck.
duien
I have done something similar, though I used Perl to create the ics files. As noted above this is pretty easy given the text based, human readable format. Pretty much all the info you need is on the Wikipedia http://en.wikipedia.org/wiki/ICalendar page. The only other wrinkle, apart from aforementioned timezone issues, that comes to mind is correctly handling event updates if you will ever need to change anything once it is published.
Quinbus Flestrin
Ok, the Stack Overflow link is helpful but - forgive my rudimentary knowledge - I'm still not quite clear on quite how to fit it into the framework I have. I have a concept of the iCal format, and a vague idea how I can use some of the PHP I already have to format the database fields into that format. What I'm missing is how to set up the file creation code surrounding some link text (or image - I'm imagining the little standard iCal button). Here, http://pastebin.com/Mf37mM8N and I wrote "CODE TO CREATE ICAL DOWNLOAD GOES HERE" where I think it should go. Any pointers would be appreciated!
dnash
You might want to have a look http://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calen. Set your headers before outputting anything, then output your correctly formatted content.
juv3nal
oh jeez I'm a dumb. that's the same link duien mentioned. See the code in that stack overflow answer? paste all that in a separate file and call it "make_my_ical_file.php" or something. Then from your page just have a "<a href="make_my_ical_file.php">click here to get an ical file </a>" You'll still need to make changes to make_my_ical_file.php to make it output the correct stuff of course
juv3nal
juv3nal - thanks, I was just starting to grasp that that was the way things should work. What I don't grasp is how do the correct values get passed to the "make_my_ical_file.php" file? So, the webpage ends up with this table, like, May 25 - 2:00pm to 3:00pm - Event Name - Location - Description - iCal Link How is it that clicking the "iCal Link" to this separate PHP file fills that file with the times, event name, location, and description from THAT line of the table/database, and not another? Or, said another way, if I write this "make_my_ical_file.php" file so that it sets up the ical format and has something like "DTSTART:" where does that piece of code fill in the variable from?
dnash
So here is an example of one possible make_my_ical_file.php file. https://gist.github.com/artlung/5493859 is the link to the code. Should be fairly self-explanatory. This presumes that your database uses the column name "id" and that's a numeric value. If there's a different primary key for the events let me know and that can be adjusted. You should be able to plop that .php file on your server and get *something* explanatory. Set $use_database to true to actually connect to your database. Also, you can use this http://severinghaus.org/projects/icv/ to validate the url or the .ics file that is downloaded to see if it's formatted right. Now, one unknown is how ical handles timezones - whether that needs to be a UTC date or whether you can set the timezone as part of the ical itself. http://stackoverflow.com/questions/7626114/ics-timezone-not-working may be helpful there.
artlung
Don't get too hung up on making the calendar files on the fly.... It's pretty inefficient to keep recreating them when it sounds like you're only adding/changing events infrequently (once a day or less?). Just write a script to make all the calendars once (offline on your own computer, even) and re-run as necessary when you add new events. Make the file names database IDs and it will be simple to link them where you need them.
anaelith
Artlung - thanks, that's fantastic. I gave your example a quick test run this morning using just the dummy data (no database connection) and to text file, and it appeared to work. A question though about how the "make_my_ical_file" is supposed to connect to the database? In the sample code I showed from my site, I left out that there's an include file that contains database path, username and password to connect to MySQL. Would that info need to be in the "make_my_ical_file" code also? (my assumption is yes...) If so, as an include? Or put the actual contents of the include file right in it?
dnash
Related Q & A:
- How To Build Business Directory Using Php Mysql?Best solution by Stack Overflow
- How to prevent duplicate entries in MySQL and PHP?Best solution by Stack Overflow
- how to create iCal feed .net?Best solution by Drupal Answers
- How to write a query for PHP and MySQL?Best solution by Stack Overflow
- How to create a table in PHP with MySQL?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.