Jump to content

Ajax & optimizing large amount of data


AP81

Recommended Posts

I know that Ajax isn't really good for large amounts of data, but I often use Ajax to populate tables that may have some 20 to 30 rows.  I do something like this:


for ($i=0; $i<$this->GetRowCount(); $i++)
{
$line = $this->GetRow();

if ($i==0) 
	$client_log .= $line["ID"];
else 
	$client_log .= "*^*" . $line["ID"];

$client_log .= "*|*" . $line["DUE_DATE"];
$client_log .= "*|*" . $line["CLIENT_CODE"];
$client_log .= "*|*" . $line["DEAL_TYPE"];
$client_log .= "*|*" . $line["PURCH_RENT"];
$client_log .= "*|*" . $line["CHANGED_DUE_DATE"];
$client_log .= "*|*" . $line["CHANGED_PROCESSED_DATE"];
$client_log .= "*|*" . $line["CHANGED_RECURRING_REVENUE"];
$client_log .= "*|*" . $line["REVENUE_DUE_DATE"];
$client_log .= "*|*" . $line["REVENUE_PROCESSED_DATE"];
$client_log .= "*|*" . $line["REVENUE"];
$client_log .= "*|*" . $line["COMMENTS"];
}
echo $client_log;
[code]

All this does is split up database rows such that fields are separated by '*|*' and rows are separated by '*^*' .  I then use the split function to split on *^* to break the data up into rows, then use the split function again on *|* to break up the fields.

So my question is, is there a more efficient way to do this?

[/code]

Link to comment
Share on other sites

Yes, transfer the data as JSON. PHP5+ has functions to output a php array as JSON.

 

So your PHP code would just be:

 

$data = array();
for ($i=0; $i<$this->GetRowCount(); $i++){
  $data[] $this->GetRow();
}
echo json_encode($data);

 

As far as how to receive it, that will depend on how you are doing your AJAX calls. Most libraries (dojo, jquery, etc) have functions for JSON data being returned. If you are doing the calls the old fashion way, you can just use eval to store it into a variable:

 

var rval; //Data returned by ajax call
eval('var data = '+rval+';');
//Now you have a JavaScript object called data to work with

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.