0

我正在开发一个用户界面,“仪表板”,上面有一些 div 框,其中包含与当前登录用户相关的信息。他们的日历、待办事项列表和一些从谷歌电子表格中动态提取的统计数据。

我在这里找到: http ://code.google.com/apis/spreadsheets/data/3.0/reference.html#CellFeed 可以使用如下网址从工作表中请求特定的单元格:
spreadsheets.google.com/feeds/cells/0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/1/public/basic/R3C2

我简要地研究了 Zend GData,但它似乎比我试图做的要复杂得多。

因此,我编写了两个 php 函数:(in hours.php)
1.)file_get_contents()根据参数行、列和工作表执行生成的 url
2.) 使用循环中的第一个来查找关联的列号使用给定的名称。

所以基本上我使用 jQuery 执行 ajax 请求,如下所示:

//开始js函数

function ajaxStats(fullname)
{
    $.ajax({
        url: "lib/dashboard.stats.php?name="+fullname,
        cache: false,
        success: function(html){
            document.getElementById("stats").innerHTML = html;
        }
    });
}

//结束js函数

// 开始文件 hours.php

<?php 
function getCol($name)
{
    $r=1;
    $c=2;
    while(getCell($r,$c,1) != $name)
    {    $c++;    }
    return $c;
}

function getCell($r, $c, $sheet)
{
    $baseurl = "http://spreadsheets.google.com/feeds/cells/";
    $spreadsheet = "0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/";
    $sheetID = $sheet . "/";
    $vis = "public/";
    $proj = "basic/";
    $cell = "R".$r."C".$c;

    $url = $baseurl . $spreadsheet . $sheetID . $vis . $proj . $cell . "";
    $xml = file_get_contents($url);

    //Sometimes the data is not xml formatted,
    //so lets try to remove the url
    $urlLen = strlen($url);
    $xmlWOurl = substr($xml, $urlLen);

    //then find the Z (in the datestamp, assuming its always there)
    $posZ = strrpos($xmlWOurl, "Z");
    //then substr from z2end
    $data = substr($xmlWOurl, $posZ + 1);

    //if the result has more than ten characters then something went wrong
    //And most likely it is xml formatted
    if(strlen($data) > 10) 
    {
        //Asuming we have xml 
        $datapos = strrpos($xml,"<content type='text'>");
        $datapos += 21;
        $datawj = substr($xml, $datapos);
        $endcont = strpos($datawj,"</content>");
        return substr($datawj, 0,$endcont);
    }
    else
        return $data;
} 
?>

//结束时间.php

//开始dashboard.stats.php

<?php
session_start();
// This file is requested using ajax from the main dashboard because it takes so long to load,
// as to not slow down the usage of the rest of the page.

if (!empty($_GET['name']))
{
    include "hours.php";
    // GetCollumn of which C#R1 = users name
    $col = getCol($_GET['name']);
    // then get cell from each of the sheets for that user,
    // assuming they are in the same column of each sheet
    $s1 = getcell(3, $col, 1);
    $s2 = getcell(3, $col, 2);
    $s3 = getcell(3, $col, 3);
    $s4 = getcell(3, $col, 4);
    // Store my loot in the session varibles,
    // so next time I want this, I don't need to fetch it
    $_SESSION['fhrs'] = $s1;
    $_SESSION['fdol'] = $s2;
    $_SESSION['chrs'] = $s3;
    $_SESSION['bhrs'] = $s4;
}
//print_r($_SESSION);
?>
<!-- and finally output the information formated for the widget-->
<strong>You have:</strong><br/>
<ul style="padding-left: 10px;">
    <li>        <strong><?php echo $_SESSION['fhrs']; ?></strong> fundraising hours<br/></li>
    <li>earned $<strong><?php echo $_SESSION['fdol']; ?></strong> fundraising<br/></li>
    <li>        <strong><?php echo $_SESSION['chrs']; ?></strong> community service hours<br/></li>
    <li>        <strong><?php echo $_SESSION['bhrs']; ?></strong> build hours <br/></li>
</ul>

//结束dashboard.stats.php

我认为我失去 4 秒的地方是getCol()[hours.php]中的 while 循环
我该如何改进这一点,并减少我的加载​​时间?

我应该放弃这个,然后去 Zend GData 吗?
如果是那个while循环,我是否应该尝试将电子表格中的每个用户列号存储在也验证登录的用户数据库中?

4

1 回答 1

0

我在 while 循环中没有适当的中断,即使找到合适的人,它也会继续循环。

加上请求需要时间去谷歌电子表格。每个请求大约 0.025 秒。

我还与 ZendGdata 的用户交谈过,他们说请求并没有快多少。

于 2010-11-29T00:24:30.517 回答