2010-08-24 29 views
0

我正在使用一个用户界面,其中包含一些div框的“仪表板”,其中包含与当前登录用户有关的信息。他们的日历,待办事项列表以及从Google电子表格中动态提取的一些统计信息。Ajax请求需要6秒才能完成,不知道为什么

我发现这里: 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功能:(在hours.php)
1)不生成的URL的file_get_contents(),基于所述参数的行,列和片
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; 
} 
?> 

// END hours.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() while循环[hours.php ]
我该如何改善这一点,并减少我的加载​​时间?

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

+1

我没有在hours.php中看到while循环...: -/ – 2010-08-24 13:42:14

+0

@Doug:由于格式化问题,它被隐藏了。 – 2010-08-24 13:58:36

+1

你真的需要打电话才能逐个获取文档吗?做这么多的服务调用是昂贵的。最小化它。 – epascarello 2010-08-24 14:06:51

回答

0

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

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

我也与ZendGdata的用户交谈过,他们说这个请求没有太快。

相关问题