php - Using Google Sheets as a database?
I would like to create a very simple PHP page for a site, which would show a timetable / calendar like data, where each slot would be either free or would have some appointment in it.
Because all the data is actually just one table, something like {month, day, hour, talk_name, talk_description}, I thought why not use a Google Sheets Spreadsheet as the database. OK, the main reason is that I'm just reading books about how to use MySQL in PHP, so I'm definitely not on a level to:
- create a nice admin interface for managing the events
- make the whole thing safe (I mean all my idea about safety is to use .htaccess for an admin folder and make the site read-only elsewhere).
On the other hand everyone could use Google spreadsheets for editing the table, so this way both the security aspects and the UI aspects would be solved.
My question is that how would you recommend me to do that? Google Sheets can both publish in XML and CSV formats. Can I just usefgetcsv
to get the data? Can you give me some simple examples how to parse the csv, and if it would be efficient (ok, it will be less than 50 views a day), if I would do something like this (sorry for the abstract syntax)?
$source_csv = fgetcsv(...);
get_talk_name(x,y,z) {
for all rows in $source_csv {
if (month == x && day == y && hour == z) return talk_name
}
}
get_talk_desc(x,y,z) {
for all rows in $source_csv {
if (month == x && day == y && hour == z) return talk_name
}
}
Answer
Solution:
So, while it might not be wise or scalable, yes, you can do it. Try this code:
Basically, publish the spreadsheet as public, change output to csv (from the default HTML) by manually editing the URL (ie,
output=csv
), fetch it, and then iterate over it line by line using.
If the spreadsheet looks like this:
This will output the following for the csv in question:
Answer
Solution:
You could try this just for fun. But if you just need a communal calendar, use something like Google Calendar. The Google Calendar API enables you to update your calendar from a program. And you can embed a calendar in your website using the Google Embeddable Calendar Helper.
Not as much fun as programming it from scratch though ... ;-)
Answer
Solution:
Check out this page for a pretty straightforward approach to using a GDocs spreadsheet as a CRUD DB. Usage follows this pattern, but you need to download a Zend class, and a GDocs/PHP file from github first...
Answer
Solution:
I don`t have enough rep to add a comment above, but the new method of exporting a sheet to CSV does work.
Your url as (publicly) shared from sheets:
https://docs.google.com/spreadsheets/d/9999999999999/edit?usp=sharing
Change the end
/edit?usp=sharing to /export?format=csv
Like:
https://docs.google.com/spreadsheets/d/99999999999999/export?format=csv
Source: https://productforums.google.com/d/msg/docs/An-nZtjaupU/llWy4eYFywcJ