2010-08-24 34 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的用戶交談過,他們說這個請求沒有太快。

相關問題