开发者

How to encode large HTML lists for jQuery?

开发者 https://www.devze.com 2023-04-13 04:05 出处:网络
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.

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 mem开发者_开发问答ory limit?).

Thanks a lot, Fela


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.


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.


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.


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/


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

0

精彩评论

暂无评论...
验证码 换一张
取 消