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
https://forums.phpfreaks.com/topic/99088-ajax-optimizing-large-amount-of-data/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.