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

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

问题描述

我想为网站创建一个非常简单的PHP页面,它会显示一个时间表/日历类似的数据,其中每个插槽将是免费的或将有一些约会。



因为所有的数据实际上只是一个表,像{month,day,hour,talk_name,talk_description},我想为什么不使用Google文档电子表格作为数据库。好的,主要原因是我只是在读书关于如何使用MySQL在PHP中,所以我肯定不是在一个水平:




  • 为管理事件创建一个不错的管理界面

  • 使整个事情安全(我的意思是我所有关于安全的想法是使用.htaccess作为管理文件夹,


  • 另一方面,每个人都可以使用Google Spreadsheets来编辑表格,因此这两种安全方面并且UI方面将被解决。



    我的问题是,你会如何推荐我这样做? Google文档可以以XML和CSV格式发布。我可以只使用fgetcsv来获取数据吗?你能给我一些简单的例子如何解析csv,如果它是高效的(确定,它将少于50次查看每天),如果我会做这样的事情(对于抽象语法) p>

      $ source_csv = fgetcsv(...); 

    get_talk_name(x,y,z){
    for $ source_csv {
    if(month == x&& day == y&& hour == z)return talk_name
    }
    }

    get_talk_desc(x,y,z){
    $ source_csv {
    if (month == x&& day == y&& hour == z)return talk_name
    }
    }


    解决方案

    所以,虽然它可能不明智或可扩展,但是,你可以做到。请尝试以下代码:

     <?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行in row $ row:< br />< / p> \\\

    $ row ++;
    for($ c = 0; $ c <$ num; $ c ++){
    echo $ data [$ c]。 < br /> \\\
    ;
    }
    }
    fclose($ handle);
    }

    基本上,将电子表格发布为public,将输出更改为csv HTML)通过手动编辑URL(即 output = csv ),获取它,然后逐行使用 fgetcsv



    如果电子表格如下所示:





    这将输出以下关于csv的问题:

      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
    }


    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 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:

    • 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 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>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
    }
    

    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:

    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文档作为数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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