I’m thinking about making a basic PHP page for a website. It would show a calendar with time slots that are either open or have appointments. The data is pretty simple - just one table with month, day, hour, event name, and description.
I’m new to MySQL and PHP, so I’m not ready to make a fancy admin interface or handle security properly. That’s why I’m wondering if I could use Google Sheets as the database instead. It would be easier for people to edit the data and solve some security issues.
Has anyone tried this before? Google Sheets can export data as XML or CSV. Could I use PHP’s fgetcsv function to get the info? I’m worried about how well it would work, even though the site won’t get much traffic.
Here’s a rough idea of what I’m thinking:
$data = fgetcsv($file);
function getEventName($month, $day, $hour) {
global $data;
foreach ($data as $row) {
if ($row[0] == $month && $row[1] == $day && $row[2] == $hour) {
return $row[3];
}
}
}
function getEventDetails($month, $day, $hour) {
global $data;
foreach ($data as $row) {
if ($row[0] == $month && $row[1] == $day && $row[2] == $hour) {
return $row[4];
}
}
}
Does this approach make sense? Any tips or better ways to do this?
Using Google Sheets as a database for a simple calendar app is definitely possible. I’ve implemented something similar for a small community project. While it’s not ideal for large-scale applications, it can be a good starting point for your needs.
One approach I found effective was using the Google Sheets API with PHP. It offers more flexibility than CSV parsing and allows for real-time updates. You’ll need to set up OAuth2 authentication, but there are good tutorials available.
Consider caching the data locally to improve performance and reduce API calls. Also, structure your sheet wisely - use separate worksheets for different data types if needed.
Remember, this solution has limitations. It may not handle concurrent edits well and could slow down with large datasets. But for a low-traffic site, it’s a pragmatic choice that balances ease of use with basic functionality.
I’ve actually used Google Sheets as a database for a small project before, and it can work surprisingly well for simple applications like your calendar app. One advantage is that it’s really easy for non-technical users to update the data.
Instead of using CSV, I’d recommend looking into the Google Sheets API. It’s more robust and gives you real-time access to the data. You can use PHP libraries like Google_Client to interact with it.
A few things to keep in mind:
There are rate limits on API calls, so it might not scale well for high-traffic sites. You’ll need to set up authentication, which adds some complexity. Also, performance might be slower compared to a traditional database.
For your use case, it sounds like a good solution to get started quickly. Just be prepared to migrate to a proper database if your needs grow beyond what Sheets can handle efficiently.
As for your code, consider using associative arrays or objects instead of numeric indexes for better readability and maintainability.