5

I have a PHP script that fetches a relatively large amount of data, and formats it as HTML unordered lists for use in an Ajax application.

Considering the data is in the order of tens to possibly more than a hundred KB, and that I want to be able to differentiate between the different lists with Javascript, what would be the best way to go about doing this?

I thought about json_encode, but that results in [null] when more than a certain amount of rows are requested (maybe PHP memory limit?).

Thanks a lot, Fela

2
  • I've noticed json_encode errors with character types. I haven't been able to narrow it down, but when specific characters are passed for encoding, the result is null. Oct 16, 2011 at 17:53
  • Ah, that must be it then. Well I've gone with a temporary solution by inserting an HTML tag between results, and using JS to split at those locations. Seems to work okay :-) Oct 16, 2011 at 18:14

5 Answers 5

2

Certain illegal characters in the string could be breaking the json_encode() function in PHP which you will need to sanitize before this will work correctly. You could do this using regular expressions if this becomes a problem.

However, if you are sending requests with that amount of data it may be unwise to send this using AJAX as your application will seem very unresponsive. It may be better to get this data directly from the database as this would be a far faster method although you will have to obviously compromise.

1
  • Sorry for not getting back to you for so long. There's no alternative to AJAX in this case (unless I do some hacky multiple-iframe job). Anyway it's mostly used from within the local intranet, and as for the few people who access it over the web, that's what the spinner gifs are for :) Oct 25, 2011 at 2:02
1

I cannot click up, but I agree with Daniel West.

Ensure your strings are UTF-8 encoded or use mysql_set_charset('utf8') when you connect. The default charset for mysql is unfortunately Latin/Windows. Null is the result of a failed encoding because of this, if it was out of memory, the script itself would fail.

0

I would pass the data around via JSON, and do it in small batches that you can present incrementally. This will make it appear faster and may get around the memory issues you are having. If the data above the scroll ( first 40 lines or so ) loads quick, it is ok if the rest of the page takes several seconds to load. If you want to get tricky, you can even load the first page and then wait for a scroll event to load the rest, so you don't have to hit the server too much if the user never scrolls to look at the data below the scrollbar. If php is returning null from the json_encode it is because of invalid characters. If you cant control the data, you could just send HTML from the server to the client and avoid all the encoding/decoding, but this means more data to transfer.

1
  • My implementation with HTML dumping seems to work okay. Load times are quick enough (less than two seconds, often much less). Also, I'm pretty sure filling an element with HTML is less computationally expensive than going through a JSON array and turning it into elements. Oct 29, 2011 at 0:04
0

With jquery you can transform your unordered list into a javascript array in your ajax application.

$.map( $('li'), function (element) { return $(element).text() });

Also, underscorejs as some very neat functions for javascript arrays and collections. http://documentcloud.github.com/underscore/

0

I would prefer JSON, and I thought the 'null' you get from PHP encode is resulted from jSON's double escaping mechanism with javaScript. I have explained it in another post.

You need to double escape special character(One suspicious 'null' cause is '\n' in your case)

json parse error with double quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.