使用 Google Docs 作为数据库? [英] using Google Docs as a database?

查看:30
本文介绍了使用 Google Docs 作为数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个网站创建一个非常简单的 PHP 页面,它会显示一个时间表/日历之类的数据,其中每个位置要么是免费的,要么有一些约会.

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.

因为所有的数据实际上只是一张表,比如 {month, day, hour, talk_name, talk_description},我想为什么不使用 Google Docs 电子表格作为数据库.好吧,主要原因是我只是在阅读有关如何在 PHP 中使用 MySQL 的书籍,所以我绝对不在以下水平:

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 Docs 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 definately not on a level to:

  • 创建一个漂亮的管理界面来管理事件
  • 让整个事情变得安全(我的意思是我所有关于安全的想法都是将 .htaccess 用于管理文件夹,并使网站在其他地方只读).

另一方面,每个人都可以使用 Google 电子表格来编辑表格,这样一来,安全方面和 UI 方面都可以得到解决.

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.

我的问题是你会如何推荐我这样做?Google Docs 可以以 XML 和 CSV 格式发布.我可以只使用 fgetcsv 来获取数据吗?你能给我一些如何解析 csv 的简单例子,如果它会有效(好吧,一天的浏览量会少于 50 次),如果我会做这样的事情(对不起抽象的语法)?

My question is that how would you recommend me to do that? Google Docs can both publish in XML and CSV formats. Can I just use fgetcsv to get the datas? 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
  }
}

推荐答案

所以,虽然它可能不明智或无法扩展,是的,你可以做到.试试这个代码:

So, while it might not be wise or scalable, yes, you can do it. Try this code:

<?php
$url = "https://spreadsheets.google.com/pub?hl=en&hl=en&key=0AupgXsRU8E9UdC1DY0toUUJLV0M0THM4cGJTSkNSUnc&output=csv";
$row=0;

if (($handle = fopen($url, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>
";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />
";
        }
    }
    fclose($handle);
}

基本上,将电子表格发布为公开,通过手动编辑 URL(即,output=csv)将输出更改为 csv(从默认 HTML),获取它,然后遍历它行按行使用 fgetcsv.

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 fgetcsv.

如果电子表格如下所示:

If the spreadsheet looks like this:

这将为所讨论的 csv 输出以下内容:

This will output the following for the csv in question:

array(2) {
  [0]=>
  string(4) "Name"
  [1]=>
  string(5) "Value"
}
array(2) {
  [0]=>
  string(3) "Foo"
  [1]=>
  string(5) "13123"
}
array(2) {
  [0]=>
  string(3) "Bar"
  [1]=>
  string(3) "331"
}

这篇关于使用 Google Docs 作为数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆