Note that there are some explanatory texts on larger screens.

plurals
  1. POAn Ajax request is taking 6 seconds to complete, not sure why
    primarykey
    data
    text
    <p>I am working on a user interface, "dashboard" of sorts which has some div boxes on it, which contain information relevant to the current logged in user. Their calendar, a todo list, and some statistics dynamically pulled from a google spreadsheet.</p> <p>I found here: <a href="http://code.google.com/apis/spreadsheets/data/3.0/reference.html#CellFeed" rel="nofollow noreferrer">http://code.google.com/apis/spreadsheets/data/3.0/reference.html#CellFeed</a> that specific cells can be requested from the sheet with a url like this:<br> <code>spreadsheets.google.com/feeds/cells/0AnhvV5acDaAvdDRvVmk1bi02WmJBeUtBak5xMmFTNEE/1/public/basic/R3C2</code></p> <p>I briefly looked into Zend GData, but it seemed way more complex that what I was trying to do. </p> <p>So instead I wrote two php functions: (in hours.php)<br> 1.) does a <code>file_get_contents()</code> of the generated url, based on the parameters row, column, and sheet<br> 2.) uses the first in a loop to find which column number is associated with the given name.</p> <p>So basically I do an ajax request using jQuery that looks like this: </p> <p>// begin js function </p> <pre><code>function ajaxStats(fullname) { $.ajax({ url: "lib/dashboard.stats.php?name="+fullname, cache: false, success: function(html){ document.getElementById("stats").innerHTML = html; } }); } </code></pre> <p>// end js function </p> <p>// begin file hours.php</p> <pre><code>&lt;?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) &gt; 10) { //Asuming we have xml $datapos = strrpos($xml,"&lt;content type='text'&gt;"); $datapos += 21; $datawj = substr($xml, $datapos); $endcont = strpos($datawj,"&lt;/content&gt;"); return substr($datawj, 0,$endcont); } else return $data; } ?&gt; </code></pre> <p>//End hours.php </p> <p>//Begin dashboard.stats.php</p> <pre><code>&lt;?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); ?&gt; &lt;!-- and finally output the information formated for the widget--&gt; &lt;strong&gt;You have:&lt;/strong&gt;&lt;br/&gt; &lt;ul style="padding-left: 10px;"&gt; &lt;li&gt; &lt;strong&gt;&lt;?php echo $_SESSION['fhrs']; ?&gt;&lt;/strong&gt; fundraising hours&lt;br/&gt;&lt;/li&gt; &lt;li&gt;earned $&lt;strong&gt;&lt;?php echo $_SESSION['fdol']; ?&gt;&lt;/strong&gt; fundraising&lt;br/&gt;&lt;/li&gt; &lt;li&gt; &lt;strong&gt;&lt;?php echo $_SESSION['chrs']; ?&gt;&lt;/strong&gt; community service hours&lt;br/&gt;&lt;/li&gt; &lt;li&gt; &lt;strong&gt;&lt;?php echo $_SESSION['bhrs']; ?&gt;&lt;/strong&gt; build hours &lt;br/&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>//end dashboard.stats.php</p> <p>I think that where I am loosing my 4 secs is the while loop in <code>getCol()</code> [hours.php]<br> How can I improve this, and reduce my loading time? </p> <p>Should I just scrap this, and go to Zend GData?<br> If it is that while loop, should i try to store each users column number from the spreadsheet in the user database that also authenticates login? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload